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

shift

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements are true?
1) System.out.println( -1 >>> 2);will output a result larger than 10
2) System.out.println( -1 >>> 2); will output a positive number
3) System.out.println( 2 >> 1); will output the number 1
4) System.out.println( 1 <<< 2); will output the number 4
Can any one please explain to me how this shift works?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ify
numbers 1, 2, and 3 are all true. 4 is false because there is no operator '<<<'.
for 1 and 2 that is the unsigned right shift, the bits are shifted to the right and filled in with 0s. So, in both of them you start with -1, which is
11111111 11111111 11111111 11111111
when you shift it right 2 places you get:
00111111 11111111 11111111 11111111
Because the sign bit (the furthest to the left) is 0 the number is positive (if it were a 1 then the number would be negative).
The value of 10 in binary is
00000000 00000000 00000000 00001010
so the number you got after shifting is much larger than 10.
Here are some other links to shift related topics:
http://www.javaranch.com/ubb/Forum33/HTML/002817.html
http://www.javaranch.com/ubb/Forum24/HTML/011093.html
And then there is the campfire story too:
http://www.javaranch.com/campfire/StoryBits.jsp

hope this helps
------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Dave Vick (edited September 21, 2001).]
 
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
Hi Ify,
1. -1 => 11111111111111111111111111111111
-1 >>> 2 => 00111111111111111111111111111111
so the result is larger than 10, answer is true
2. -1 as above
-1 >>> 2 => as above
the result is positive since the MSB is 0 and not 1
answer is true
3. 2 => 00000000000000000000000000000010
2 >> 1 => 00000000000000000000000000000001
which is equal to 1, answer is true
4. 1 => 00000000000000000000000000000001
1 <<< 2 will not compile since <<< is not a valid Java operator
HIH
Val
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys and special thanks to you Dave for the wonderful explanation and on the links.
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic