Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# What is happenning with bits ?

Max Bazhenov
Greenhorn

Joined: Oct 30, 2004
Posts: 18
byte b = -128;
b = (byte) (b >> 1);

The result is negative !
STEP 1: -128 = 1111 1111, correct ?
STEP 2: promote byte to int
result: 1000 0000 0000 0000 0000 0000 0111 1111, correct ?
STEP 3: shift
result: 1100 0000 0000 0000 0000 0000 0011 1111, correct ?
STEP 4: norrowing cast
result: 0011 1111, correct ?

Why do we get negative as a result, where is the error ?

[ November 04, 2004: Message edited by: Max Bazhenov ]
[ November 04, 2004: Message edited by: Max Bazhenov ]
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
The result is negative !
STEP 1: -128 = 10000000,
STEP 2: promote byte to int
result: 1111 1111 1111 1111 1111 1111 1000 0000,
STEP 3: shift
result: 1111 1111 1111 1111 1111 1111 1100 0000, correct ?
STEP 4: norrowing cast
result: 1100 0000, Ans -64.

FYI: negative numbers represented in 2's complement form
Max Bazhenov
Greenhorn

Joined: Oct 30, 2004
Posts: 18
damn, don't get it.
what is 2's complement form ?
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Max,

To get two's complement always first write one's complement and add one to it.
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
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.
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
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

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.

hope this helps a tad bit
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271

SCJP Tipline, etc.

Have you checked out Aspose?

subject: What is happenning with bits ?