# Zero fill right shift ">>>"

Mindy Wu
Ranch Hand
Posts: 121
I understand how "<<" works, but I have a hard time to understand how ">>>" works, can anyone use the following example to explain to me how does it work?
int b = -11; //11111111 11111111 11111111 1111 0101
b = b >>> 2; // b now becomes 00111111 11111111 11111111 1111 1101

(I understand when the operand is posoitive but I have no idea when it is negative.)
Mindy

Marcellus Tryk
Ranch Hand
Posts: 64
When you use the >> operator the vacated bits on the left are filled with the original sign bit (leftmost bit). If you use the >>> operator they're filled with 0.
E.g.
10111010 >> 4 = 11111011
10111010 >>> 4 = 00001011
If the number being shifted is positive (leftmost bit is 0) then >> and >>> behave the same - in both cases 0 filled.

kate damond
Ranch Hand
Posts: 77
Hi,
I know that all the books talk about this >>, >>> stuff, but I seem to have mssed the point of it all. How does it work, and why would you want to use it ?
Thanks,
Kate

Richard Boren
Ranch Hand
Posts: 233
Here is very good JavaRanch Campfire story covering bitwise operations Cat and Mouse Games with Bits.
[This message has been edited by Richard Boren (edited June 12, 2001).]

Mindy Wu
Ranch Hand
Posts: 121
Hi Tod,
Thank you for your example! What I want to know is how does it work when the left value is negative?

Originally posted by Tod Tryk:
When you use the >> operator the vacated bits on the left are filled with the original sign bit (leftmost bit). If you use the >>> operator they're filled with 0.
E.g.
10111010 >> 4 = 11111011
10111010 >>> 4 = 00001011
If the number being shifted is positive (leftmost bit is 0) then >> and >>> behave the same - in both cases 0 filled.

[This message has been edited by Mindy Wu (edited June 12, 2001).]

Manfred Leonhardt
Ranch Hand
Posts: 1492
Hi Mindy,
Tod's example not only uses a negative value but your question is answered in text ...
Read it again, only slower this time and let it sink in ...
Regards,
Manfred.

Mindy Wu
Ranch Hand
Posts: 121
Hi Manfred,
I still don't understand how
10111010 >> 4 = 11111011 works, i am going to read the "operations Cat and Mouse Games with Bits" article. By the way, can you tell me how to convert negative number into binary format?
For example:
0 0000
1 0001
2 0010
3 0011
..
I don't know how to get the binary bit for negative number.
Originally posted by Manfred Leonhardt:
Hi Mindy,
Tod's example not only uses a negative value but your question is answered in text ...
Read it again, only slower this time and let it sink in ...
Regards,
Manfred.

[This message has been edited by Mindy Wu (edited June 12, 2001).]

Mindy Wu
Ranch Hand
Posts: 121
Thanks all, finally I got it!
Mindy

[This message has been edited by Mindy Wu (edited June 12, 2001).]

Mike Curwen
Ranch Hand
Posts: 3695
This is for Kate...

Here is a discussion on bit shifting. http://www.javaranch.com/ubb/Forum33/HTML/002109.html