Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] increasing a dword so that it doesn't wrap around!

Author
Message
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 19th Oct 2010 11:28
There's a problem with incrementing certain datatypes... After a while, all the bits flip back around. Say you have an eight bit number:
11111110
that number is 254.
11111110+1=11111111 = 255 base ten
however since we only have eight bits to store our number in...
11111111+1=00000000 = 0
in other words, 255+1=0.

To prevent this, you have to take extra precautions; like this code I made for incrementing Dwords, so (with the analogous case with our eight bit number) 255+1=255 and not 0, and 0-1=0, and not 255.



Useful if you want to...

uh... I'll get back to you on that.

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 20th Oct 2010 18:30
good one this, but it does not work with negative numbers, could it?

Dark Physics makes any hot drink go cold.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 20th Oct 2010 22:08
Quote: "Useful if you want to...

uh... I'll get back to you on that."


Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 20th Oct 2010 23:17 Edited at: 20th Oct 2010 23:17
Actually, I think it can work with integer too!
i can't test it out right now, but this should work:


Actually, whenever I write some quick and dirty code to convert variables to RGB values, I use bytes and cap them at 255 using similar code.

for any signed or unsigned binary number, this should work (reeplacing the placeholders):

probably won't work with floats tho.

Login to post a reply

Server time is: 2024-03-29 11:53:07
Your offset time is: 2024-03-29 11:53:07