• 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

Bit shifting question

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers

Please explain me why

public class test
{
public static void main(String args[])
{
int i = 1;
System.out.println("i="+~i);
i= ~i>>1;
System.out.println("i="+i);
}
}

ans is -2 and -1 for System.out.println stmts i m bit confuse why its like that according to me it should 0 and large positive integer correct me.

Thanks.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Easiest way for compliment of a number is negate the number and add one to it.Therefore for 1 the compliment is -2. and for shift operator >> u divide the number by 2 to the power of 1 (which is the right hand operator of >> ... it is simple u get -1 as answer...

i=1;
~i= -(1+1)ie is equal to -2.
then i>>1 ie..-2>>1 that comes -2/(2 to the power of 1)
=>-2/2
=>-1
hence the output -2 and -1

regards
vandana
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, 1, in in binary is this:

00000000 00000000 00000000 00000001

So, if we want ~1, we flip all the bits:

11111111 11111111 11111111 11111110

As it turns out, that's the binary representation of -2. So, that explains the initial output of -2.

Next, we take that inverse and shift it to the right one position (using a signed shift). A signed right shift means that, whatever the sign bit is prior to the shift, that's what will get shifted in from the left. So, taking the above inserve and shifting to the right one position, we're left with this:

11111111 11111111 11111111 11111111

And that is the representation of -1, which explains the latter half of the output.

Here are some more articles you can read regarding bit operations and two's complement:

Negative Numbers in Java - Two's Complement
Bit-Ops (AND, OR, XOR, NOT)
Bit-Ops (Bit Shifting)

Also, I believe bit operations, such as these, no longer appear on the 1.5 version of the exam, although they are a part of the 1.4 version.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at the actual bits for the variable i should help with this. To make it simple assume integers are 8 bits here. With the first assignment to i we have


The first println prints ~i which is:



In two's complement form this represents -2 so the println is correct. Notice that the ~ operator is simply negating every bit.

Then the signed right shift by 1 bit occurs leaving us with:



In two's complement form this represents -1 so that's what gets printed.

I don't always remember how to convert to two's complement form (I know it's add or subtract 1 and then invert but I don't remember which) but in this case you don't have to. The ~ operator only inverts bits. The interpretation of them is two's compliment and I'm assuming that the JVM gets it right. I'm certain, though, that "1111 1110" doesn't represent "0" so I don't see how ~i (when i=1) would possibly be "0".

_M_
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic