In this lesson, we will see how we can evaluate the highest/lowest value of an int in C language. Unlike C++ and Java, this is not as straight forward as it may seem. To understand the value of the discussion below one should have a preliminary knowledge of 2's compliment arithmetic. 2's compliment arithmetic will not be discussed here. The general idea is this.

Any numbers in a modern arithmetic is represented in 2's compliment form. if a computer system is n bit wide then the highest number is $$2^{n-1}-1$$ and the lowest number is $$2^{n-1}$$

Generally (not always), modern computers are 32 bit wide. So, highest positive number that int can represent or hold is $$2^{32-1}-1$$ Similarly the lowest negative number representable in int32 is $$2^{32-1}$$

Now, lets focus on representing this highest positive number in C. That should be simple as given below:

[code language="C"]

int INT_MAX = (1<<31)-1;

[/code]

But this code, results into an -Woverflow error. So what went wrong?

To evaluate (1<<31)-1, this is what the computer does: It takes the 1 and left shifts it by 31 bits.

[code language="C"]

00000000 00000000 00000000 00000001

[/code]

on left shifting by 31 bits, we get

[code language="C"]
10000000 00000000 00000000 00000000
[/code]

and now subtract -1 from it. -1 in 2s compliment form is

[code language="C"]
11111111 11111111 11111111 11111111
[/code]

and now the following addition takes places

[code language="C"]
10000000 00000000 00000000 00000000
11111111 11111111 11111111 11111111
[/code]

Now, clearly the addition above will result in a 33 bit number, hence a overflow.

Now to evaluate the expression (1<<31)-1 correctly in C, we have to store the result in a data type bigger then 32 bits. For this, we use long data type.

[code language="C"]
long x = 1;
long INT_MAX = (x&lt;&lt;(sizeof(int)*8)-1)-1;

printf("%lu\n", INT_MAX); //outputs 2147483647 for 32 bit machine
[/code]

[latex]