File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# bit representation of integers

harsha p reddy
Greenhorn

Joined: Jun 22, 2005
Posts: 2
Hello,

I am kind of breaking my head trying to figure out how a bit representation
1111 1111 1111 1111 1111 1111 1111 1111 will be equal to -1.

Actually this is one of the exercises in Sun Certified Programmer and Developer for Java 2 by Kathy Sierra and Bert Bates (Exercise 3 - 1 to be precise). The program is as follows.

Class BitShift
{
public static void main(String args[])
{
int x = 1;
x = x << 1; // shifting left by 31 bits
x = x >> 1; // shifting right by 31 bits
System.out.println("After both the shifts, x: " + x);
}
}

If I do manually I get the above value (all 32 bits 1). But I can't figure out how this is equal to decimal value -1. Kathy and Bert must be obviously right and even when I ran the program I get output as x = -1. I guess I am messing up somewhere in converting the bit form to decimal form. For example I don't know how 1111 1111 1111 1111 1111 1111 1111 1110 is equal to decimal value -2. I am sorry if this is such a dumb question. But I just can't get it. Can someone please help me...

Thanks,
harsha.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24149

27

The easy answer is that it is, by definition. It's called "two's complement notation", and the basic idea is that to reverse the sign of a number, you invert all the bits and add one. So 0001 (+1) becomes 1110 plus one equals 1111 (or -1); 0010 (+2) becomes 1101 plus one equals 1110 (-2), etc.

Dave Wood
bronco
Ranch Hand

Joined: Aug 02, 2004
Posts: 161
Just a quick note on this topic:

The 5.0 exam (CX-310-055) does NOT have questions about the shift operators!

Write code that correctly applies the appropriate operators including assignment operators (limited to: =, +=, -=), arithmetic operators (limited to: +, -, *, /, %, ++, --), relational operators (limited to: <, <=, >, >=, ==, !=), the instanceof operator, logical operators (limited to: &, |, ^, !, &&, ||), and the conditional operator ( ? : ), to produce a desired result. Write code that determines the equality of two objects or two primitives.

Co-Author of <a href="http://www.oreilly.com/catalog/jswing2" target="_blank" rel="nofollow">Java Swing</a><br />Co-Creator of <a href="http://www.sun.com/training/catalog/courses/CX-310-055.xml" target="_blank" rel="nofollow">SCJP 5.0</a> and <a href="http://www.sun.com/training/certification/java/associate_beta.xml" target="_blank" rel="nofollow">SCJA</a> exams
bhavesh bhanushali
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
does this mean that all negative numbers in java are represented in two's complement form .

1 = 0001
reverse all bits i get --> 1110 --> ( 14 in decimal format )
add one to the resultant value --> 1111 ( 15 in decimal format )

so finally -1 in decimal format would be 15

is this correct , please explain

regards ,
bbv
Sangita Mishra
Greenhorn

Joined: Jun 15, 2005
Posts: 22
The leftmost bit always represents the sign of the number.
It should left out when finding out the value.
So if the leftmost bit is 1 then it's a -ve number.
For -ve number you have to find the number by 2's complement method (NOT as you calculate foe +ve numbers). ex:

14 in binary is 00001110 (if it is a byte)
-14 in binary is 11110010
harsha p reddy
Greenhorn

Joined: Jun 22, 2005
Posts: 2
Thanks a bunch guys. You were all such a great help.

-harsha.

I agree. Here's the link: http://aspose.com/file-tools

subject: bit representation of integers