posted 13 years ago
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.
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.
posted 13 years ago
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.
You can read more about this, for example here.
posted 13 years ago
Just a quick note on this topic:
The 5.0 exam (CX310055) 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.
The 5.0 exam (CX310055) 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.
CoAuthor of <a href="http://www.oreilly.com/catalog/jswing2" target="_blank" rel="nofollow">Java Swing</a><br />CoCreator of <a href="http://www.sun.com/training/catalog/courses/CX310055.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
posted 13 years ago
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
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
posted 13 years ago
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
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
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
Rocket Oven Kickstarter  from the trailboss
https://coderanch.com/t/695773/RocketOvenKickstartertrailboss
