• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to understand shifting in Java.

 
Femi Alla
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case it's not clear - "most significant bit" means the leftmost bit, and "least significant bit" is the rightmost bit.
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read this campfire story?
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
Instead, the result is -1.
To produce a value of 127, you actually have to shift the byte 25 times to the right. Try running the following.

I think I will add the above to my mock exam. Thanks for the idea.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic