The amount to shift is taken to be the modulus 32 (for int; 64 for long) of the argument, so x >> -3 is taken as x >> 29. By the same token, x >> 35 becomes x >> 3. And watch out for x >> 32; this is a no-op. Interesting that the vm accomplishes this 'modulus' by simply masking off all but the low 5 bits (6 for long) of the argument, and that this works for negative as well as positive values. [ November 19, 2003: Message edited by: Steve Lovelace ]

Originally posted by Steve Lovelace: The amount to shift is taken to be the modulus 32 (for int; 64 for long) of the argument, so x >> -3 is taken as x >> 29. By the same token, x >> 35 becomes x >> 3. And watch out for x >> 32; this is a no-op.

steve, what is x here? both positive and negative ?? Is the rule for all types of shifts?? [ November 19, 2003: Message edited by: dimple kaushik ]

Hi!!! It is true that 35%3 is 32. But how does -3%32 become 29?I don't understand. -3%32 will be -3 right?

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270

posted

0

Hi Sagarika , You are right. -3 % 32 = -3

[ November 19, 2003: Message edited by: Cathy Song ]

Aleksandar Stojanovic
Greenhorn

Joined: Sep 08, 2003
Posts: 26

posted

0

Wait a minute please . I would like to ask something. As we said 4 << 35 = 4 << 3, because 35%32 = 3, am I correct ??? but now lets see problem from the begining. How should i calculate 4 << -35 ??? Here I've got an example :

public class Test { public static void main(String[] args){ byte b = (byte)( 7 >> -6 ); System.out.println(b); } } and output from it is 0. ??? Is anyone there who could exmplain me step by step shifting where additiveExmpression is negativ. Thanks in advance

Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39

posted

0

Hi! This is regrading your question about (7>>-6)

The binary representation of 7 is 111 The binary rep of -6 is 1111 1111 1111 1111 1111 1111 1111 1010 The least 5 significant bits of -6 is 11010 which is equal to 26 So 111>>26 =0 b'cos all the ones are shifted out after shifting them just thrice which leaves us with just zeroes even if they are cast to a byte. I hope I am right friends.