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
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# Shift operators

Greenhorn
Posts: 8
• Number of slices to send:
Optional 'thank-you' note:
-31>>>5>>5>>>5>>5>>>5>>5
This will give a 3 as result.
-31 will be 30 when inverted and sign taken off and now moved right 30 times.
I do not understand how we still end up as 30
Could some one come up with a step by step explanation.
Would appreciate it greatly
Thanks

Author & Gold Digger
Posts: 7617
6
• Number of slices to send:
Optional 'thank-you' note:
this will be evaluated from left to right i.e.
(((((-31>>>5)>>5)>>>5)>>5)>>>5)>>5
-31 = 11111111111111111111111111100001
Now,
-31 >>> 5 = 00000111111111111111111111111111
00000111111111111111111111111111 >> 5 = 00000000001111111111111111111111
00000000001111111111111111111111 >>> 5 = 00000000000000011111111111111111
00000000000000011111111111111111 >> 5 = 00000000000000000000111111111111
00000000000000000000111111111111 >>> 5 = 00000000000000000000000001111111
00000000000000000000000001111111 >> 5 = 00000000000000000000000000000011
which is equivalent to 3 !
HIH
Val
[This message has been edited by Valentin Crettaz (edited September 24, 2001).]

Harsha Jay
Greenhorn
Posts: 8
• Number of slices to send:
Optional 'thank-you' note:
Thank you very much
but is there a method i can call in the program to get the internal representation of the int. I'm sure i saw it somewhere.

Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
• Number of slices to send:
Optional 'thank-you' note:
you mean the binary representation of an int ?
Integer.toBinaryString("-31");
returns
11111111111111111111111111100001
Val

Ranch Hand
Posts: 3141
• Number of slices to send:
Optional 'thank-you' note:
harsh,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.