• 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

Java Ranch Mock Exam Question

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear sir ,
Attached here is a question from java ranch mock exam which i had a problem with.I was under the impression that the bits wrap around (that is why 5>>32 gives 5 only & 5>>33(ie.33%32=1)is the same as 5>>1).What i am having a problem understanding is that,how does the value remain the same after shifting 32 bits(the size of an int).As mentioned in the mock exam the answer is (b).But,if the bits are gone then how is the same value retained after shifting 32 bits .Pls let me know .Thanks.
Java Ranch Mock Exam Question :
********************************
(#205)When using the right shift operator(>>),what happens to the bits which move off the right end ?
(a)They wrap around
(b)They'e gone
(c)They are added to result
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Elementary dear, x << y is actually compiler treats it as x << (y%32) if x is an int and x << (y%64) if x is a long.
Now if someone says 5<<32 that means 5<<0 (no shift taking place)
and 5<<34 means 5<<2 (left shift by two).
Hope this solves your question........
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
although amonds answer is correct, i think you question reveals what is so great about java. The compiler + JVM is so smart it realizes that shifting an int by 32 is no shift at all so it retains the value 5 instead of filling in 0's.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or im full of it!
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the description does say its a mod32 operation so it just subtracts 32 from what you give it.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic