• 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 representation of integers

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

You can read more about this, for example here.
 
bronco
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a bunch guys. You were all such a great help.

-harsha.
 
reply
    Bookmark Topic Watch Topic
  • New Topic