|
|
Re: Question of the Day 2
|
Posted: Aug 1, 2002 9:36 AM
|
|
> public static void main (String[] s) { > byte b = 5; > System.out.println(b<<33);
Here b is byte. However, byte is promoted to int before any manipulation. Since, int has only 32 bits, you cannot shift its bits by more than 31. Here, 33 is int and if you read JLS carefully then it says in case of i << n, where n is int, only first 5 lower order bits of n will be used as shifting no. It is same as saying for all positive n , replace n by n % 32. Here b << 33 will be replaced by b << ( 33 % 32 ) i.e. b << 1. So your answer is 10. If n is long then first six lower order bits will be used as shift no.
Thanks Kishori
|
|