04-26-2006, 08:04 PM
Heres a couple i came across recently that i found quite interesting. They are for testing if an addition or subtraction causes an overflow (ie the result is out of signed range)
These examples assume that a and b are 16-bit signed values. This can be changed by changing the AND &H8000 to other values (ie &H80 for 8-bit).
Here is a link to a topic where redcrab explained to me exactly how they work
http://www.freebasic.net/forum/viewtopic.php?t=3914
Addition
Subtraction
These examples assume that a and b are 16-bit signed values. This can be changed by changing the AND &H8000 to other values (ie &H80 for 8-bit).
Here is a link to a topic where redcrab explained to me exactly how they work
http://www.freebasic.net/forum/viewtopic.php?t=3914
Addition
Code:
overflow = ((a XOR (NOT b)) AND (a XOR (a + b)) AND &H8000) <> 0
Subtraction
Code:
overflow = ((a XOR b) AND (a XOR (a - b)) AND &H8000) <> 0