• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Bit shifting?

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

answer 2,4
here how can the answer be 4...
my doubt is here the long variable after moving ..how can it be assigned it to int ...without explicit casting ....
please clarify

thanks
sri
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The long is *not* being assigned to anything. It is merely being used to to state how many bits to shift. And even in this regard, only the lower 5 bits of the long will be used.

No assignment of the long. Hence, no casting necessary.

Henry
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
And even in this regard, only the lower 5 bits of the long will be used.



What's meant with that sentence?

because i tought the number of bits that will be shifted is 3 (99 % 32 (= max bits of an int)), so have no idea what's meant with lower 5 bits of long will be used
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

because i tought the number of bits that will be shifted is 3 (99 % 32 (= max bits of an int)), so have no idea what's meant with lower 5 bits of long will be used



99 & 31 = 3

99 in binary:
00000000 00000000 00000000 01100011

31 in binary:
00000000 00000000 00000000 00011111

When they are ANDed together, the result is (in binary):
00000000 00000000 00000000 00000011

Or, 3. This is how everything but the five lowest bits are masked off. It's mathematically equivalent to %32, but the JLS specifically describes the behavior as using the five lowest bits for int values (six lowest for long).

Hope this helps...
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And for those of you studying for the Tiger exam, remember this topic doesn't apply to you, it's not on the Tiger exam!
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Morrow:
Hope this helps...



it did help.
thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic