• 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

Binary

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.?
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jone,I think that does not answer my question.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic