• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

adding byte values question...

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main( String[] args )
{
byte b = 127;
byte c = (byte)(b + 1);
System.out.println(b);
System.out.println(c);
}
When I compile and run this, it displays c as -128. Why is it doing this?
byte b is 127 which is 01111111
how does adding 1 to it gives 10000000.
I am bit confused on this, appreciate your help.
Thanks in advance.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you add 00000001 to 01111111, you get this:

Of course, in Java, the leftmost bit is a sign bit. It only tells you if a number is positive or negative. If it's a 0, the number is positive and, if it's a 1, the number is negative. Therefore, our addition is rolling us over from the maxiumum value you can contain in a byte (127) to the minimum value you can contain in a byte (-128).
If you do a search in this forum on 2's complement, I'm sure you can find a lot of good descriptions of this.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jay Patel:
public static void main( String[] args )
{
byte b = 127;
byte c = (byte)(b + 1);
System.out.println(b);
System.out.println(c);
}
When I compile and run this, it displays c as -128. Why is it doing this?
byte b is 127 which is 01111111
how does adding 1 to it gives 10000000.
I am bit confused on this, appreciate your help.
Thanks in advance.



You're getting overflow, bytes can store numbers from -128 to 127.
127=0111 1111
0111 1111
+0000 0001
----------
1000 0000

To read the number apply the 2's complement rules:
1) complement: ~1000 0000 = 0111 1111
2) add 1: 0111 1111 + 1 = 1000 0000
3) read the number: 1000 0000 = 128
4) add the sign and you get: -128.

HTH,
Bojan
[ February 25, 2004: Message edited by: Bojan Knezovic ]
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot you'll
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic