To get two's complement always first write one's complement and add one to it. Start with +128 which can be written as 0000 0000 0000 0000 0000 0000 1000 0000 ---> +128 to get -128 flip all the bits and then add 1 to the result 1111 1111 1111 1111 1111 1111 0111 1111 ---> one's complement adding 1 to the above 1111 1111 1111 1111 1111 1111 1000 0000 ---> this is -128 now lets move one bit to the right >> 1 1111 1111 1111 1111 1111 1111 1100 0000 ---> this is -64 The byte representation would be 1100 0000 --> -64

Hope this helps ya.

[ November 04, 2004: Message edited by: Jay Pawar ] [ November 04, 2004: Message edited by: Jay Pawar ]

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.

welcome to this sparse club... I too once was a first class victim of this bit game and got walloped on a number of occasions till some generous tips by yoda masters on this ranch came by .

Here's the rant:-

STEP 1: -128 = 10000000,

STEP 2: (byte)(b >> 1)

makes life somewhat easy, that we only need to go for the last 8 bits (phew !), which at this moment is:

10000000

and the 'shift >> 1' results in:

11000000 ; the extreme right '0' drops off a cliff and meets a horrible death and the space created on the extreme left gets gobbled up with a '1'

STEP 3 : moment one sees that the extreme left side is a '1', it's always a (-)ve #. (take it for granted)

so apply a thumb rule here:

(i) flip the bits 00111111 (ii) add 1

00111111 00000001 + ___________ 01000000

in this funky arithmetic[ called 2's complement ...which I never got at school ] 01+01 = 10, '0' gets put down and the '1' gets carried over.

the final '01000000' amounts to 64 , but STEP 3 sez: it's a (-)ve #, so to top the icing, the answer = - 64.