Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binary

 
fraternity lawson
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jone,I think that does not answer my question.
 
Seema Das
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic