• Post Reply Bookmark Topic Watch Topic
  • New Topic

Byte  RSS feed

 
Kora Darmstaedter
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

can anybody help me with the following example I found in a book:

byte b2 = (byte) 200;
System.out.println(b2);

The output is "-56" and I don't understand why.
The binary for 200 is 11001000, the first digit is interpreted as the sign.
Remainder: 1001000. Which is 72 and not 56.
So why is the result "-56" and not "-72".

Thank a lot for your help in advance!
Kora
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remainder: 1001000

No, this does not work like that. You have to substract 1, and inverting all bits (two's complement):
11001000 - 1 : 11000111
11000111 invert : 00111000 (56)
 
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
No, this does not work like that. You have to substract 1, and inverting all bits


Traditional, with Twos complement, to negate a number, you invert then you add one. This should work in both directions -- obviously, as negate is an operation that works in both directions.

As Christophe said, you can also "subtract one and invert", which is the reverse of "invert and add one", so that should work too. And work in both directions.

Anyway, IMO, it may be easier to take the "add one" route, as adding is sometimes easier than subtraction.

[EDIT: Fixed name. Sorry, Christophe]

Henry
[ October 01, 2008: Message edited by: Henry Wong ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to expand a little...

the reason for using "two's complement" is to avoid a situation where you have two values for "0".

If we follow your methodology (i.e. using the high bit for the sign, and the rest for the value), then both "0000 0000" and "1000 0000" would evaluate to "0". This makes life difficult for a lot of logic. You would have to say
if (x == 0 || x == -0)...

which doesn't make much sense.

By using two's complement, we solve the problem. to 'negate' 0, you invert all the bits, so you'd get 1111 1111, then adding one, you'd get "1 0000 0000", with the high bit falling off, putting you right back where you started. Which is how it should be.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another reason is that addition works the same for both negative and positive numbers, and even for adding a negative to a positive number, without any special cases.
 
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
Originally posted by Ilja Preuss:
Another reason is that addition works the same for both negative and positive numbers, and even for adding a negative to a positive number, without any special cases.


I think this may be the most important reason. With Twos Complement, the same logic used to add & subtract unsigned numbers can be used for signed numbers. There is no need to have different microcode for both types.

Henry
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right, thanks for pointing this out.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!