• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Zero fill right shift ">>>"

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all, finally I got it!
Mindy

[This message has been edited by Mindy Wu (edited June 12, 2001).]
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is for Kate...

Here is a discussion on bit shifting. http://www.javaranch.com/ubb/Forum33/HTML/002109.html
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic