• Post Reply Bookmark Topic Watch Topic
  • New Topic

Another overflow question.  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys just a quick question about overflow I'm using a 32 bit System I know data is stored in memory as single Bytes,so I expected to get an overflow after the number 4294967296 which is 2^32 but the overflow does not happen until well after that at 4611686018427387904 and after that which is already wrong because of the singed number -9223372036854775808 then overflow happens but how come it goes far beyond 32? or do I have this concept totally wrong which yes I probably do ?


 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A long is 64 bits.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:A long is 64 bits.


but how can my 32 bit system handle a 64 bit number?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
but how can my 32 bit system handle a 64 bit number?


32 bits at a time...

Seriously, the "32 bits" of a machine generally refers to the data bus, the addressing, and/or the registers. Variables are stored in memory. And most machines have enough memory to dedicate 64 bits to a variable.

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Adam Chalkley wrote:
but how can my 32 bit system handle a 64 bit number?


32 bits at a time...

Seriously, the "32 bits" of a machine generally refers to the data bus, the addressing, and/or the registers. Variables are stored in memory. And most machines have enough memory to dedicate 64 bits to a variable.

Henry


Yeah I had quite the feeling I was wrong because my memory is 4GB,memory part is still confusing me abit I understand what the stack and the heap is as to when methods and objects are created(heap) and how they are popped off the stack and the control is returned to the level below when its done and I also understand how memory is stored in each cell and how its treated BUT what I don't understand is why there would be a stack I thought the CPU is instructed by the program counter what memory location its next execution would be,what would the need for the stack if that's the case would it choose memory locations with the highest address to execute next then?
 
Stephan van Hulst
Saloon Keeper
Posts: 7968
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program counter only keeps track of the next instruction to perform. Jumping is done by setting the program counter. So how does Java set the program counter back to where it left when it invoked a method? It puts the address on the stack when it invokes a method, and when the method returns, it puts the address from the stack back into the program counter. This way you can have many nested method calls, without the machine getting confused about what to execute next. The stack also keeps track of the values of local variables, because when a method returns, you also need your old variables back.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote: . . . I expected to get an overflow after the number 4294967296 which is 2^32 . . .
Why? An int is a signed number so you should get overflow after 2³°, so you expect to go
1073741824→2147483648→0
for 32 bits. Note the middle number is negative.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why? An int is a signed number so you should get overflow after 2³°, so you expect to go
1073741824→2147483648→0
for 32 bits. Note the middle number is negative.


Cambell, I am not exactly sure what you are trying to say... but the point where an int value overflows should be from 2147483647, and it should overflow to the value of -2147483648 (as you mentioned).

Henry
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam,

And you could read more about it, right here (<- link).
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: . . . but the point where an int value overflows should be from 2147483647, and it should overflow to the value of -2147483648 (as you mentioned).

Henry
You only see 2147483647 if overflow occurs by adding 1. OP was repeatedly doubling numbers so he would double 1073741824 and get -2147483648, and I showed the output he would expect from repeated doublings.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Adam,

And you could read more about it, right here (<- link).
So I did and it says you can have unsigned integers. Well, well, well, well, well!
I don't know how, yet, but it says to look up the Number classes. That might mean you can use Integers in such a way as not to get overflow until you reach 4294967295, but that would overflow to 0 if you add 1.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!