Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Paul Clapham
• Devaka Cooray
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Frits Walraven
Bartenders:
• Carey Brown
• salvin francis
• Claude Moore

# Trying to understand shifting in Java.

Ranch Hand
Posts: 79
Hello,
I have not been able to understand fully the unsignied right shift (>>> operator and the whole issue of shifing negative numbers.
I know that negative numbers have 'ones' instead of 'zeros' in binary form. But does "most significant position" mean and how do I implement the shifts on negative numbers?
The authors of R & H book and all the other books I've used must've felt the meaning of twos compliment operators was so obvious that they didn't even bother to explain it. I don't know what that means: Can anyone please explain this to me too?
Thanks.

Ranch Hand
Posts: 1865
To convert a positive number to a negative number represented in two's compliment format, just invert each bit and add one. Do the same to convert from negative to positive.

Dan Chisholm
Ranch Hand
Posts: 1865
Maybe I should give you a few examples of converting positive to negative and vice versa. I will use eight bit bytes just to keep things simple.
+1 = 00000001;
-1 = 11111111;
Notice that the most significant bit (the first bit on the left) of positive one is zero. Notice that the most significant bit (msb) of negative one is a one. That's the sign bit.
If you add the positive one and the negative one the result is 00000000 as you would expect. Of course, you have to accept the fact that we ignore the bit that overflows as a result of the addition.

Dan Chisholm
Ranch Hand
Posts: 1865
The most significant bit of a number represented in two's compliment format is the sign bit. As a result, positive number can become negative number when they become too large. If one is added to a byte that previously held the value 127, then the byte will become negative 128.
+127 = 01111111
If you add one, the result is
-128 = 10000000
The above is an obvious example of why the range of a byte is -128 to +127.

Wanderer
Posts: 18671
In case it's not clear - "most significant bit" means the leftmost bit, and "least significant bit" is the rightmost bit.

Sheriff
Posts: 9099
12
Have you read this campfire story?

Dan Chisholm
Ranch Hand
Posts: 1865
The behavior of the shift operator is fairly obvious when working with 32 bit integers. However, the behavior of the shift operator is not entirely obvious when working with smaller primitive types such as byte, short, and char. That's because the shift operator promotes the operands to int primitives before doing the shift. For example, you would expect the following code to print a value of 127.
byte b = -1;
b = (byte)(b>>>1);
System.out.println(b);