• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

About unsigned right shift operator

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ranchers,

It seems that 128>>>2 results in 32. Could you please explain me how do we calculate this?

Mnay thanks,
Sreedhar
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used this formula #/2^n where # is the number and n is the shift number, so we get 128/2^2 = 128/4 = 32. Since the # is not a negative number, the sign will not change, giving you a positive result.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... 128 64 32 16 8 4 2 1

0 ... 1 0 0 0 0 0 0 0

"sign bit" .... more bits

32 bits long --------------->

If you displace the previous number with >>> the unsigned right shift operator you move the 1 two places which result in 32.

Remember the >> "normal" right shift operator does not displace the leftmost bit (sign bit).
[ February 15, 2007: Message edited by: Javier Sanchez Cerrillo ]
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java integers are stored as a 32-bit integer which is binary like a lot of zeros from which the last part of 128 is like.

1000 0000 which is 2^7 = 128.

You're using a unsigned right shift operator so al the bytes are moved 2 place to the right which means

0010 0000 which is 2^5 = 32.

When you use the right shift operator (>> the sign bit will be the same. This means a negative number will stay negative. Using the unsigned right shift operator a negative number will be positive because the sign bit will also be replaced with a zero. In your example the orginal value was positive so using the signed or the unsigned operator has no influence.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic