public class Bits { public static void main(String[] args) { int a = 256; // 100000000 int b = 32; // 100000 System.out.println (a >>> b); } }

1 Compilation succeeds, the output is 0 2 Compilation succeeds, the output is 32 3 Compilation succeeds, the output is 256 4 Compile time error 5 None of the above

I'm sorry, but what's your question? If you want the answer, just try compiling it and see what happens. If you're confused by what really does happen, let me know what you're confused about and I'll try to explain.

Thnx for too quick responce Actually i was trying to edit and somehow my account was freezed for few mins and couldn't do that immediately. I answered it as (1) but the ans is (3) The explanation given is- Before the unsigned right shift operator takes place, a mod 32 is performed in this case the % 32 has 0 as remainder, meaning 0 places to shift, so the answer is equals to the value on the left hand side of the operator.This effect kicks in only when the left operand is 32, as the remainder is 0, 0 places are shifted. I never heard of performing mod 32 for shifting operators. So i was little confused and wanted to know all such special cases. Corey, ofcourse i tried compiling it and even tired for -ve numbers and tried for all valid examples with %32 being 0. Though had 2yrs of working exp in java never came across shift operators. so maybe i'm still learning abcd's ThanQ

Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504

posted

0

Kate, you can't shift bits by a number larger than there are bits in the left operand. I don't like using the remainder operator for shifting, especially if you're to shift by a negative number like this: 256>>>-1; Instead, you can grab the rightmost five bits of the number to be shifted. -1 is 1111 1111 1111 1111 1111 1111 1111 1111 in binary. If you grab the 5 bits to the right, you get 31 (int is 32 bits). And 256>>>31 is 0. You take 6 rightmost bits for long numbers, which gives you a maximum of 63 bits (long is 64 bits). You can get away with the remainder operator for positive numbers. Some special cases of shifting include shifting by multiples of either 32 for int or 64 for long. The left operand remains the same in these cases.