# Thread: [RESOLVED] Loop number over

1. ## [RESOLVED] Loop number over

Hello all! I am not sure if I can properly describe this. I want a function that takes a long and if it is over 255, loops over back to 1. e.g. Whatever(255) = 255; Whatever(256) = 1.

If it is a negative number, it does opposite. Whatever(0) = 0; Whatever(-1) = 255. I've gotten close to this but not quite. I am losing my mind over this and I'm sure it something really simple that I missed. I will be calling this function enough that nothing slow will work.

Thank you all in advance, help is much appreciated!!

(would this question be a math question or VB question??)

2. ## Re: Loop number over

I'm not sure what you want for negative numbers but for positive numbers it should be Whatever(i) = i mod 256. Modular arithmetic works within a confined interval.

3. ## Re: Loop number over

Originally Posted by VanGoghGaming
I'm not sure what you want for negative numbers but for positive numbers it should be Whatever(i) = i mod 256. Modular arithmetic works within a confined interval.
Shouldn't that be i mod 255?
Whatever(256) = 1.
If i=256 --> 256 mod 255 --> Result = 1

And agreed: No idea how negative numbers are supposed to work

4. ## Re: Loop number over

Originally Posted by Zvoni
Shouldn't that be i mod 255?
255 doesn't satisfy his condition "Whatever(255) = 255". LoL, I think the specifications should be clearer.

5. ## Re: Loop number over

Originally Posted by VanGoghGaming
255 doesn't satisfy his condition "Whatever(255) = 255". LoL, I think the specifications should be clearer.
Right, i walked into that one

Code:
```Sub main()
Dim i As Long
For i = 0 To 258
Debug.Print "i=" & i & " - Result: " & i \ 256 + i Mod 256
Next
End Sub```
Result:

i=250 - Result: 250
i=251 - Result: 251
i=252 - Result: 252
i=253 - Result: 253
i=254 - Result: 254
i=255 - Result: 255
i=256 - Result: 1
i=257 - Result: 2
i=258 - Result: 3

6. ## Re: Loop number over

Looks spot on!

7. ## Re: Loop number over

Originally Posted by Zvoni
Right, i walked into that one

Code:
```Sub main()
Dim i As Long
For i = 0 To 258
Debug.Print "i=" & i & " - Result: " & i \ 256 + i Mod 256
Next
End Sub```
Result:

i=250 - Result: 250
i=251 - Result: 251
i=252 - Result: 252
i=253 - Result: 253
i=254 - Result: 254
i=255 - Result: 255
i=256 - Result: 1
i=257 - Result: 2
i=258 - Result: 3

if you change 258 to 1000 , it give incorrect data. i put 1000 and on concurrent loops, then next time will start at 2 then next will start at 3

Could use below. a little bit slower

Dim i As Long, ReLoop As Long
For i = 0 To 1000

If ReLoop = 256 Then ReLoop = 1

Debug.Print "i=" & i & " - Result: " & ReLoop
ReLoop = ReLoop + 1
Next

8. ## Re: Loop number over

Originally Posted by k_zeon
if you change 258 to 1000 , it give incorrect data. i put 1000 and on concurrent loops, then next time will start at 2 then next will start at 3

Could use below. a little bit slower

Dim i As Long, ReLoop As Long
For i = 0 To 1000

If ReLoop = 256 Then ReLoop = 1

Debug.Print "i=" & i & " - Result: " & ReLoop
ReLoop = ReLoop + 1
Next
Agreed. Didn't consider that.
Though, it's not making much sense:
for i=511 it jumps back to 1 which is not making sense, since 511 is not 2x256

But that's OP's Problem, not ours

9. ## Re: Loop number over

Originally Posted by OrangeBlaze
Hello all! I am not sure if I can properly describe this. I want a function that takes a long and if it is over 255, loops over back to 1. e.g. Whatever(255) = 255; Whatever(256) = 1.

If it is a negative number, it does opposite. Whatever(0) = 0; Whatever(-1) = 255. I've gotten close to this but not quite. I am losing my mind over this and I'm sure it something really simple that I missed. I will be calling this function enough that nothing slow will work.

Thank you all in advance, help is much appreciated!!

(would this question be a math question or VB question??)
Whatever(255)=255
Whatever(256)=1
Whatever(257)=2
...
Whatever(510)=255
Whatever(511)=1
Whatever(512)=2

Is this really what you want?
The more logical way is to loop 0-255, not 1-255.
But if so, your formula for X>255 is (X - 256) Mod 255 + 1
And for X<0 is X And 255

10. ## Re: Loop number over

I use this function for those situations:

Code:
```Function Modulus(Numerator As Long, Modulo As Long)
Modulus = ((Numerator Mod Modulo) + Modulo) Mod Modulo
End Function```
Modulus(1,256)=1
Modulus(-1,256)=255

Since normal Mod doesn't handle negatives correctly for my most common uses.

11. ## Re: Loop number over

Originally Posted by Dry Bone
But if so, your formula for X>255 is (X - 256) Mod 255 + 1
And for X<0 is X And 255
Yes, this is what works perfectly for me!! Thank you and everyone else who helped!!

12. ## Re: [RESOLVED] Loop number over

Or just:

Code:
```
y = (x And 255) Mod 256
```
Although, looking at your OP, you say that 256 --> 1. It seems to me that 256 should go to 0, which is what the above will do.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•