This week's book giveaway is in the Python forum.We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Paul Clapham
• Jeanne Boyarsky
Sheriffs:
• Devaka Cooray
• Junilu Lacar
• Tim Cooke
Saloon Keepers:
• Tim Moores
• Ron McLeod
• Tim Holloway
• Claude Moore
• Stephan van Hulst
Bartenders:
• Winston Gutkowski
• Carey Brown
• Frits Walraven

# bit representation of integers

Greenhorn
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.

author and iconoclast
Posts: 24203
43
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.

bronco
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.

Ranch Hand
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

Greenhorn
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
Posts: 2
Thanks a bunch guys. You were all such a great help.

-harsha.

 Don't get me started about those stupid light bulbs.