Originally posted by ram shah:
henry,
in the rarlier discussion only the left hand operand was negative. here, both operands are negative. I've doubt regarding this situation only. How do we right shift or left shift a number(positive or negative) by a larger negative number?
For shifting of ints only the lower 5 bits of right operand is significant (for longs, it is the lower 6 bits). Everything else from the previous discussion remains the same.
8 = 0000 0000 0000 0000 0000 0000 0000 1000
negate 8 = 1111 1111 1111 1111 1111 1111 1111 0111
twos comp 8 = 1111 1111 1111 1111 1111 1111 1111 1000 = -8
33 = 0000 0000 0000 0000 0000 0000 0010 0001
negate 33 = 1111 1111 1111 1111 1111 1111 1101 1110
twos comp 33 = 1111 1111 1111 1111 1111 1111 1101 1111 = -33
low 5 bits of -33 = 0000 0000 0000 0000 0000 0000 0001 1111 = 31
-8 = 1111 1111 1111 1111 1111 1111 1111 1000
-8 >>> 31 = 0000 0000 0000 0000 0000 0000 0000 0001 = 1
Henry