hi javaranchers ! i am not able understand what's happening when RHS of the binary shift operands is negative ? (say 2 >> -2) . It does not give a compile error ! can anyone explain the logic behind the output of such operations? thanks sivaram

Hi Sivaram, When the value is positive we can say that for integers the shift amount is: range modulus 32 and for long: range modulus 64. When the value is negative we can say the following for integers: (32 - range) modulus 32 and for long: (64 - range) modulus 64. Therefore, we can say that the following 2 statements are equivalent: 1 << -2; 1 << 30; Regards, Manfred.

Hi Manfred, Can you give more examples or any references to this negative shifting?Thank You.

Regards Steffy

sing
Ranch Hand

Joined: Nov 29, 2001
Posts: 121

posted

0

Hi Manfred, Can you give more examples or any references of this negative shifting? Thank You. Regards, Steffy

Originally posted by Manfred Leonhardt: Hi Sivaram, When the value is positive we can say that for integers the shift amount is: range modulus 32 and for long: range modulus 64. When the value is negative we can say the following for integers: (32 - range) modulus 32 and for long: (64 - range) modulus 64. Therefore, we can say that the following 2 statements are equivalent: 1 << -2; 1 << 30; Regards, Manfred.