## Integer Arithmetic Overflow and Underflow

### Integer Arithmetic Program:

```#include
#include
#define Imax 0X7FFFFFFF
#define Imin 0X80000000
main()
{
long int Z;
double fImin,fImax;

printf("Integer Arithmetic Overflow and Underflow Output:\n\n");

printf("Defined Hexdecimal (Binary not Available) Constants:\n\n");
printf("#define Imax 0X7FFFFFFF\n");
printf("#define Imin 0X80000000\n\n");

printf("Imin = %#010x (hex) = %d (dec);\n",Imin,Imin);
printf("Imax = %#010x (hex) = %d (dec);\n\n",Imax,Imax);

fImin = -pow(2,31);
fImax = pow(2,31)-1;

printf("integer(-2^(31) ) = 1000000000000000000000000000000 (binary by string constant);\n");
printf("integer(2^(31)-1) = 1111111111111111111111111111111 (binary by string constant);\n\n");

printf("double(-2^(31) ) = %13.1f (dec);\n",fImin);
printf("double(2^(31)-1) = %13.1f (dec);\n\n",fImax);

Z = Imax+1;
printf("Compiler Gives Warning Message for `Z = Imax+1;'!:\n");
printf("`intarith.c:28: warning: integer overflow in expression ';\n");
printf("But NOT Upon Execution!\n\n");
printf("Imax+1 = %#010x (hex) = %d (dec) = Imin;\n\n",Z,Z);

Z = Imin-1;
printf("Imin-1 = %#010x (hex) = %d (dec) = Imax;\n\n",Z,Z);

printf("So Integer Arithmetic has a Circular Distribution: Imax+1=Imin!\n\n");

printf("Note: there is no binary printf format in C.\n");
} /* end main procedure */
```

### Integer Arithmetic Output:

```Integer Arithmetic Overflow and Underflow Output:

Defined Hexdecimal (Binary not Available) Constants:

#define Imax 0X7FFFFFFF
#define Imin 0X80000000

Imin = 0x80000000 (hex) = -2147483648 (dec);
Imax = 0x7fffffff (hex) = 2147483647 (dec);

integer(-2^(31) ) = 1000000000000000000000000000000 (binary by string constant);
integer(2^(31)-1) = 1111111111111111111111111111111 (binary by string constant);

double(-2^(31) ) = -2147483648.0 (dec);
double(2^(31)-1) =  2147483647.0 (dec);

Compiler Gives Warning Message for `Z = Imax+1;'!:
`intarith.c:28: warning: integer overflow in expression ';
But NOT Upon Execution!

Imax+1 = 0x80000000 (hex) = -2147483648 (dec) = Imin;

Imin-1 = 0x7fffffff (hex) = 2147483647 (dec) = Imax;

So Integer Arithmetic has a Circular Distribution: Imax+1=Imin!

Note: there is no binary printf format in C.
```

Web Source: http://www.math.uic.edu/~hanson/mcs471/IntegerArithmetic.html

Email Comments or Questions to hanson@uic.edu