posted 16 years ago

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.

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.

SCJP

posted 16 years ago

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<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>

Dan Chisholm

Ranch Hand

Posts: 1865

posted 16 years ago
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>

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.

+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

posted 16 years ago
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>

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.

+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.

posted 16 years ago

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

"I'm not back." - Bill Harding, *Twister*

posted 16 years ago

Have you read this campfire story?

JavaBeginnersFaq

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Dan Chisholm

Ranch Hand

Posts: 1865

posted 16 years ago
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>

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.

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.

It is sorta covered in the JavaRanch Style Guide. |