aspose file tools*
The moose likes Beginning Java and the fly likes Zero fill right shift >>"" title="" /> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Zero fill right shift ">>>"" Watch "Zero fill right shift ">>>"" New topic
Author

Zero fill right shift ">>>"

Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
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.)
Thanks for your help!
Mindy
Marcellus Tryk
Ranch Hand

Joined: May 31, 2001
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

Joined: Apr 17, 2001
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

Joined: Mar 01, 2001
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

Joined: Jan 12, 2001
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

Joined: Jan 09, 2001
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

Joined: Jan 12, 2001
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

Joined: Jan 12, 2001
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

Joined: Feb 20, 2001
Posts: 3695

This is for Kate...

Here is a discussion on bit shifting. http://www.javaranch.com/ubb/Forum33/HTML/002109.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Zero fill right shift ">>>"
 
Similar Threads
hexadecimal problem
10 >> -2
bit shifting
octal/hex/binary/unicode ?
Binary