Why Absolute Value of INT_MAX and INT_MIN is different?


Q: You may already know this, the minimal number a signed 1 byte (8 bit) integer is -128 and the maximum number a signed 1 byte (8 bit) integer is 127. So the question is why the absolute value of these two bounds is not the same?  |-128| <> |127| ?

To answer this question, we have to understand the binary representation for integers in computer. Let’s take a 4-bit (half byte) integer for simplicity.

If the MSB (Most Significant Bit, the left-most bit) represents the sign (0 for positive, 1 for negative), then a four-bit integer can represent straightforward the following.

0 to 7:  +0000 to +0111

-0 to -7: +1000 to +1111

So, the problems arise.

1. The representation of zero is not unique:  it can be represented by 0000 and 1000.

2. A waste,  in theory, 2^4 = 16 numbers can be represented, but only here 15 numbers are represented (because of duplicate zero)

3. -7 is smaller than -0, but 1111 is bigger than 1000, which is not very convenient if we want to compare numbers. For example, we eventually want to subtract the numbers (including the signed bit) and compare two numbers.

So, to eliminate the above problems, we can use the Two’s Complement.

The term of One’s Complement means all bits are opposite of the original binary. For example, 0110 will become 1001

The term of Two’s Complement equals to One’s Complement Plus One, so 0110’s Two’s Complement will be 1001+1=1010

Why do we need such ‘complicate’ thing? The answer is to help computers do the subtracting. In computers, only additions are easily supported. To convert subtraction into addition, we can use Two’s Complement.

For non-negative numbers (0 to 7), the Two’s Complement stays the same, which are 0000 to 0111

For negative numbers , the Two’s Complement will be the One’s Complement Plus One which is 1001 to 1111.

negative zero = 0000 and The Two’s Complement becomes 1111 + 1 = (1)0000, the highest bit is discarded (overflow), therefore, it is the same as positive zero.

For example, if we want to compute 7 – 3, we can use Two’s Complement.

7 = 0111

-3 = ~(0011) + 1 = 1101

0111

+1101

——–

(1)100

The MSB is discarded, so the answer is 100 = 4. So the subtraction for integers can be transformed into addition base on the Two’s Complement

Let’s look that the list of new representations:

-7:  1 001
-6:  1 010
-5:  1 011
-4:  1 100
-3:  1 101
-2:  1 110
-1:  1 111
-0:  0 000
+1: 0 001
+2: 0 010


We can see that 1 000 is un-used. So we can use this (1 000) to represent either -8 or +8,  but the fact is -8 + 1 = -7, so 1000 is used to represent -8, which is better and reasonable.

So, the range for signed byte is -128 to 127, for two-bytes is -32768 to 32767 for four-bytes  is -2^31 to 2^31 – 1 and so on…

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
556 words
Last Post: Coding Exercise - Timus Online Judge - 1264. Workdays - C++ solution
Next Post: Compare A + B > C (64 bit) - Integer Overflow

The Permanent URL is: Why Absolute Value of INT_MAX and INT_MIN is different?

Leave a Reply