posted 21 years ago
hi sonir,
say u have 4-bit capacity registers. so we can have -2^3-1 to +2^3 numbers that is [-8,7] (keep it int tho).
why so? how do v decide -ve limit?
simple. comp uses 2's complement. so startout writing +ve numbers from 0 along with it's -ve equvivalent.
0 = 0000 , -0 = 0000 (which isnt really there. so we will discard -0)
1 = 0001 , -1 = 1111
2 = 0010 , -2 = 1110
3 = 0011 , -3 = 1101
4 = 0100 , -4 = 1100
5 = 0101 , -5 = 1011
6 = 0110 , -6 = 1010
7 = 0111 , -7 = 1001
so far as u can see all the -ve have 1 as Most Significant Bit (MSB). so that should mean that when we try to represent +ve 8 = 1000 it is not +ve but -ve.
so, we have +7 max and -8 min but dont have +8.
now, when we take abs() of any -ve value see what happens,
e.g. -3 = 1101. we can just do that by taking 2's complement which would be 0011 = 3. right?
now take -8 = 1000. get 2's complement. result is 1000 = -8 !!!
so, MIN_VALUE will result into the same value for int and long for this reason. and so if we do comparison with 0 it will be false for (number > 0).
hope u got this.
regards
maulin.