0

I see in the JDK that Integer.MIN_VALUE is 0x80000000. Considering that the original is 0x80000000, then the opposite is 0x8fffffff, and finally the complement is 0x8fffffff + (-1) = -2^32? So whether —2^32 is 1000 0000 0000 0000 0000 0000 0000 0000 in bit?

zhongwei
  • 325
  • 1
  • 5
  • 15

3 Answers3

5

With 32 bits you can represent 2^32 integers.

  • 2^31 are negative
  • 2^31 - 1 are positive
  • 1 is 0

If you sum them, you get 2^31 + 2^31 - 1 + 1 = 2^32.

Hence the max (most positive) integer is 2^31-1 and the min (most negative) integer is -2^31.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • So Integer.MIN_VALUE' s two's complement is 1000 0000 0000 0000...in bit? – zhongwei Mar 31 '16 at 06:53
  • Also the particular arrangement of bits makes addition extremely straightforward. For brevity I'll use a 4 bit number to explain: if 2 is represented by 0010, then -2 is represented by the flipping all the bits and adding one, so 1101 + 1 = 1110. The advantage of this is that when you then add together 2+(-2) you get 0010 + 1110 =10000 which gets truncated to 0000 because we only had 4 bits to work with. – jvalli Mar 31 '16 at 06:54
  • Thanks!Actually, my question is that where is the symbol bit in Integer.MIN_VALUE, is it the number bit's self? – zhongwei Mar 31 '16 at 07:09
1

Consider a simple 4 bit scenario:

Computer stores negative integers as 2's complement. To get a 2's complement of number we follow this procedure:

for negative 8=>
1000 (positive 8 in binary)
0111 (flip all bits=1's complement)
+ 1 (add 1)


1000 (this is how negative 8 as integer is stored in the computer, MSB=sign bit=1 indicates -ve)

therefore 2's complement=1's complement+1

1 advantage of 2's complement is that it has only 1 representation of 0 unlike 1's complement(which has +ve and -ve 0 i.e 0000 and 1111 respectively called as 0 crossing problem). Hence that is the reason you get an extra value in the negative side

so to conclude for a 4 bit scenario:

  • 0000 to 0111 means 0 to +ve 7 (MSB used as sign bit, MSB=0 means +ve)
  • 1000 to 1111 means -8 to -1(MSB=1 means -ve)
  • 0 in 2's complement is 0000, 2's complement: 0000->1111+1=10000(extra carry 1 is out of range hence it results 000) i.e only 1 representation for 0 i.e 0000

to count: 0 to 7 is 2^3-1=7 +ve
to count: -8 to -1 is 2^3=8 -ve
to count: count for 0 is 1
Sum of the counts=> 1+7+8=16=2^4

Therefore for your question: 2^31 are +ve integers and 2^31-1 are -ve integers and 1 more value for 0.

Sidenote:

converting from 2's complement: 1000->0111+1=1000(8)
value is 8 and put a -ve sign i.e final value is -8

converting from 2's complement: 1111->0000+1=0001(1)
value is 1 and put a -ve sign i.e final value is -1

riteshtch
  • 8,629
  • 4
  • 25
  • 38
0

The number of integers of a particular precision is even, since the arithmetic is based on powers of 2; and thus you cannot have as many positive integers as you have negative ones, the zero would steal one position.

bipll
  • 11,747
  • 1
  • 18
  • 32