• 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
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-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
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you mean the binary representation of an int ?
Integer.toBinaryString("-31");
returns
11111111111111111111111111100001
Val
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
harsh,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
rubbery bacon. rubbery tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic