• 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

How to get a 42 bit long number in Java.

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java's long comprises of 63 bits where in the MSB is stored for the sign.

I want to set 42nd bit of a particular number as 1.
So I tried 1<<42, however this doesnt give the output as what I thought.
neither did this work 1<<(42L).
I am not supposed to use Strings and other wrapper class functions.
Is there anyway by which I can fix this up? This is a small part of my program but a vital one.
I am getting the answer to be 1024, this very well means that it is treating it to be a 32 bit number and then 1<<10.
confused with this.
-Thank you.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you get, and what did you expect? and please post your actual code (in code tags) so we can see what you are REALLY doing.

I have a few guesses as to what the problem may be, but without seeing what you're doing, and knowing what you are expecting, they would only be guesses.

edit: I believe the problem is with your '1' literal. try declaring it as a long :

1L << 42

also.... the set bit is already in the first position, so you may need to only shift it 41.

 
Ashish Schottky
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Fred
Thanks for reply.
Well I am writing this independently and once I get this part working I will integrate it with my code.

I was expecting 4398046511104
where as I got 1024.
Edit after you edited:
Thanks a lot it solved my problem.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was going to make a suggestion, but I see Fred has already made that suggestion. So now my suggestion is to read and implement Fred's suggestion.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashish Schottky wrote:
Thanks a lot it solved my problem.


More importantly, do you understand WHY it solved your problem?
reply
    Bookmark Topic Watch Topic
  • New Topic