• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

64 bit long ??

 
Dean Jones
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I was trying to do a for loop earlier like this:

and I kept getting: integer number too large
and I realized something... How the heck can a long be 64 bits on a 32 bit machine?? I mean, in every Java book I have been reading, I haven't noticed them state anything about that. If they have, I must of skimmed over it... Which is HIGHLY possible.
However, I am curious... Is the long a pseudo 64 bit integer somehow? Or am I right in thinking that if I'm sitting on a 32 bit Intel machine, then the long can only be 32 bits and hence my compiler error?
Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dean,
Welcome to JavaRanch!
First, the syntax error: you have to write "long" constants as 50000000000000L -- that trailing "L" means it's a long, not an int.
Now, how can longs be 64 bits? The JVM specification explains that they take up two 32-bit slots on the JVM stack, or two adjacent 32-bit registers. This has some interesting consequences: for example, reading or writing to long values is not "atomic" with respect to multithreading. You have to be careful with longs or you can accidentally "see" partially-written values.
In a "real" 32-bit computer, it works much the same way (although there aren't rigid stack "slots", the registers are indeed a fixed 32 bits.) There are usually special CPU instructions that operate on longs; the register-based versions assume that multiple registers are being used to hold the value. The threading issues naturally fall out of this architecture.
 
Dean Jones
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the trailing L and F are going to end up being the death of me in Java... Thanks for your help.
 
Joe Pluta
Ranch Hand
Posts: 1376
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me it was the fact that a leading zero made things octal. Ugh.
Joe
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic