Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Binary Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Binary" Watch "Binary" New topic
Author

Binary

fraternity lawson
Greenhorn

Joined: Mar 11, 2001
Posts: 26
public class rodney
{
public static void main(String[]args)
{
int b = -11; // 11111111 11111111 11111111 1111 0101
b = b >> 2; // 11111111 11111111 11111111 1111 1101 (2's complement of -3)
System.out.println(b);
}
}
How the output is coming -3.?
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
dear lawson,
if we take a bitwise complement of bit string representation of a non negative number n,it will be the bitwise representation of -(n+1).
int a=70707;
~a is -70708
jeena jose
fraternity lawson
Greenhorn

Joined: Mar 11, 2001
Posts: 26
jone,I think that does not answer my question.
Seema Das
Ranch Hand

Joined: Mar 05, 2001
Posts: 78
Originally posted by fraternity lawson:
public class rodney
{
public static void main(String[]args)
{
int b = -11; // 11111111 11111111 11111111 1111 0101
b = b >> 2; // 11111111 11111111 11111111 1111 1101 (2's complement of -3)
System.out.println(b);
}
}
How the output is coming -3.?


Your binary representation of -11 is not correct. It should be
1111 1111 1111 1111 1111 1111 1111 0110. What you have is just complement of 11. add 0001 to it and then it becomes -11.
After shifting two bits it becomes
1111 1111 1111 1111 1111 1111 1111 1101 which is -3
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
lawson,
bitwise complement of 11 is
1111 1111 1111 1111 1111 1111 1111 0100.
add 1 to it.you get binary representation of -11.
binary representation of -11 is not correct. It should be
1111 1111 1111 1111 1111 1111 1111 0101.
After shifting two bits it becomes
1111 1111 1111 1111 1111 1111 1111 1101 which is -3
jeena jose
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Franerty,
Seema is right. Now to answer the second question as what is the number represented by
<code> 11111111 11111111 11111111 1111 1101 </code> is equal = ??

Clearly from above you can see that it is "-3"
Hope this clarifies.
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 08, 2001).]
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
The binary representation of a negative number is the
2's Complement of the positive value.
For eg: TO get the binary value of -3 take 2's complement of 3
Am i right ?
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Anjella,
You are right, first you need to take the two's compliment
and ADD 1 to the result to get the CORRECT number.
Hope this is clear now.
Ravindra Mohan.
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
Hello Ravindra,
I suppose what u are saying is take the 1's complement and then
add 1 to it (i.e. 2's complement of the number )
So suppose i want to get binary value of
a = - 3
0000 0000 0000 0000 0000 0000 0000 0011 // binary of +3
1111 1111 1111 1111 1111 1111 1111 1100 // invert bits (1's complement)
+ 1 // add 1
1111 1111 1111 1111 1111 1111 1111 1101 // After 2's complement i get binary representation for a = -3
Correct me if wrong !
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Bingo Angela,
You have understood it correctly.
In case you more interested in learning about bits-n-bytes.
Kindly visit the link to Cat and Mouse Games with Bits in camfire at Javaranch.
Please dont miss other intersting bed time stories there.
Ravindra Mohan

[This message has been edited by Ravindra Mohan (edited May 08, 2001).]
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
This little article is helpful for the most part, but either there's a typo or I'm completely missing something. The article says "But >> shifts the bits to the right using zeros to fill the leftmost bits, and when it finishes, it resets the left bit to 1 to keep the result negative."
Then every example that follows shows that each left bit is filled with ones, not zeros as the quote claims.
If the interpretation is that it fills it with a zero, then changes it to a one -- what is the point? How is that different from filling it with a one in the first place?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Binary