File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jasper Vader
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...)
 
Jasper Vader
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, totally correct!
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jasper Vader
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm...interesting, a point to remember Layne. Thanks all for the cool replies
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic