File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes After an overflow, a byte gets set to a negative value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "After an overflow, a byte gets set to a negative value?" Watch "After an overflow, a byte gets set to a negative value?" New topic
Author

After an overflow, a byte gets set to a negative value?

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
1: void infiniteLoop()
2: {
3: byte b = 1;
4:
5: while ( ++b > 0 )
6: ;
7: System.out.println("Welcome to Java");
8: }
because there is nothing there before the ';' on line six, nothing gets done while b is larger than 0. But then b gets past 127 in value and suddenly b gets reset!! And then Welcome to Java gets printed out - so B has been reset to a negative number. I am wondering what defines what 'b' gets set to. The reverse of what it initially was?
hmm...i was wondering what happens to numbers when they get too big for their boots in a counting loop.
Would this resetting process happen with a short or an int in a similar fashion?


giddee up
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Jasper,
what u r referring to is due to the rounding effect at bit level.
byte is 8 bits and so its range is -128 to +127. whenever u try to add 1 to +127 it rounds it back to -128. here is how it works,
+127 = 01111111 right?
add 1 to it,
that is 10000000 (which should be +128 , right??) but as the variable was decalred byte and we have to consider the number in 2's complement form the value for this variable would be,
-(2's complement of the answer) = -(+128) that is -128 and thats how it gets reset...
i may not be clear in explaining this bit level stuff but i tried...
can some one give more better examples for int, short etc for which similar things happen?
thanks!
maulin
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Yes, it would happen in a similar fashion.
In binary, 127 gets written as
01111111
No, if you add one, you get
10000000
For an int, this would be the representation for 128, but for bytes, the highest of the 8 bits is used as the sign bit, in the two's complements format - resulting in a value of -128. (Do a search on two's complements - there are already several threads discussing it...)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Maulin, you explained it very well indeed! And thanks also to Ilja.
So this is why the range of byte is -2(power of 7) to 2(power of 7) minus one. excellent. i see!
So I know a search on two's complement would be apropriate here, but i will just take the time to suggest this...
Seeing the range of a short is -2 (power of 15) to 2 (power of 15) minus one, if a short was counted up in an iterative loop, eventually it would get reset too...let me see here...-32,768! It would get counted up to 32,767 and then be reset (the highest of the short's 16 bits being used for the sign bit).
and an int would get reset at 2,147,483,647 back to -2,147,483,648 ....the highest of the int's 32 bits being used for the sign bit.
[ January 15, 2003: Message edited by: Jasper Vader ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Yes, totally correct!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I'd like to note that, even though this is the usual result in modern computers, I don't think the overflow result is defined by the Java language. It is poor programming practice to rely on this behavior since it COULD conceivably change some time in the future.
Keep coding!
Layne


Java API Documentation
The Java Tutorial
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Hmmm...interesting, a point to remember Layne. Thanks all for the cool replies
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: After an overflow, a byte gets set to a negative value?
 
Similar Threads
Doubt from majji test
about primitive
Doubt with ++ operator
byte
infinite loop