This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unsigned right shift Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unsigned right shift " Watch "Unsigned right shift " New topic
Author

Unsigned right shift

KATE MOORE
Greenhorn

Joined: Nov 12, 2003
Posts: 7
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
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Are you asking a question?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
KATE MOORE
Greenhorn

Joined: Nov 12, 2003
Posts: 7
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
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unsigned right shift
 
Similar Threads
tricky ques.
JTest and char initialization
Mock #2 and Question #28
Implicit Conversion?
about static variable question