programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Byte

Kora Darmstaedter
Greenhorn
Posts: 12
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".

Kora

Christophe Verré
Sheriff
Posts: 14691
16
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
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
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
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
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
That's right, thanks for pointing this out.

 Don't get me started about those stupid light bulbs.