• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is happenning with bits ?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
damn, don't get it.
what is 2's complement form ?
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Max,


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 ]
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

hope this helps a tad bit
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Negative Numbers in Java - Two's Complement

Bit-Ops (Bit Shifting)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic