Integer overflow in c





3. Cyclic nature of unsigned int:

Range of unsigned int is 0 to 653535. If we will assign a value greater than 653535 then value of variable will be changed to a value if we will move clockwise direction as shown in the figure according to number. If that number is less than 0 then we have to move in anti clockwise direction.

 


Short cut formula to find cyclic value:

If number is X where X is greater than 65535 then
New value = X % 65536
If number is Y where Y is less than 0 then
New value = 65536– (Y% 65536)

4. Cyclic nature of signed
int:

Range of unsigned int is -32768 to 32767. If we will assign a value greater than 32767 then value of variable will be changed to a value if we will move clockwise direction as shown in the figure according to number. If that number is less than -32768 then we have to move in anti clockwise direction.






Short cut formula to find cyclic value:
If number is X where X is greater than 32767 then
p = X % 65536
if p <=32767
New value = p
else
New value = p - 65536
If number is Y where Y is less than -32768 then
p = Y % 65536
If p <= 32767
New value = -p
else
New value = 65536 -p
Note: Same rule is also applicable in case of signed long int and unsigned long int.



Overflow of char data type in c programming
Overflow of int data type in c programming
Overflow of long int data type in c programming
Overflow of float data type in c programming
Overflow of enum data type in c programming
Data type tutorial home
C tutorial home.








2 comments:

Anonymous said...

Write a program in C++ to display the size of int, char, and. float data types

Unknown said...

Cyclic nature of unsigned int... All well.
Cyclic nature of int... Aaaand we're in undefined behaviour land.

http://stackoverflow.com/a/4240878/1592377