permaculture playing cards*
The moose likes Beginning Java and the fly likes How to derive Binary representation of Negative Numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to derive Binary representation of Negative Numbers" Watch "How to derive Binary representation of Negative Numbers" New topic
Author

How to derive Binary representation of Negative Numbers

Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
How can arrive at the binary representation of -192?
I understand how this works for positive numbers, but negative numbers are confusing.
Any feedback will be appreciated.
Thanks,
Vineet
Sridevi Shrikanth
Ranch Hand

Joined: Jan 11, 2001
Posts: 31
Negative numbers are represented as 2's complement.
Let me take a simple no: 41
Its binary rep is : 00101001
Taking 2's complement is as follows:
Negate the bits and add one to the result:
Negating the bits: 11010110
Add 1 : 11010111
so -41 is 11010111
Hope this helps
Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
Sridevi,
Thanks for the example. However, doing the same for -192 does not seem to work.
Can you please let me know what you arrive at as the solution.
Thanks,
Vineet
Originally posted by Sridevi Shrikanth:
Negative numbers are represented as 2's complement.
Let me take a simple no: 41
Its binary rep is : 00101001
Taking 2's complement is as follows:
Negate the bits and add one to the result:
Negating the bits: 11010110
Add 1 : 11010111
so -41 is 11010111
Hope this helps

Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Vineet.
A little rule that I use when I need to represent negative numbers in binary is
~i = -i-1.
That is, the bitwise inversion of "i" is equivalent to negative "i" less one.
In your example, you're looking for the binary representation of -192. Since -192 = -191-1, the following statement is true:
~191 = -192.
So if we can find the binary representation of 191, then invert all its bits, we will have the binary representation of -192.
This computation requires us to know which data type we are working with: since you didn't specify, I'll assume we are working with ints (32 bits).
191 = 2**7 + 2**5 + 2**4 + 2**3 + 2**2 + 2**1 + 2**0
+191 = 00000000 00000000 00000000 10111111
then
~191 = 11111111 11111111 11111111 01000000 = -192.
I hope this helps, Vineet.
Art
Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
Art,
Thanks a lot for your help. Your method makes things very easy.
Thanks again,
Vineet
Originally posted by Art Metzer:
Hi, Vineet.
A little rule that I use when I need to represent negative numbers in binary is
~i = -i-1.
That is, the bitwise inversion of "i" is equivalent to negative "i" less one.
In your example, you're looking for the binary representation of -192. Since -192 = -191-1, the following statement is true:
~191 = -192.
So if we can find the binary representation of 191, then invert all its bits, we will have the binary representation of -192.
This computation requires us to know which data type we are working with: since you didn't specify, I'll assume we are working with ints (32 bits).
191 = 2**7 + 2**5 + 2**4 + 2**3 + 2**2 + 2**1 + 2**0
+191 = 00000000 00000000 00000000 10111111
then
~191 = 11111111 11111111 11111111 01000000 = -192.
I hope this helps, Vineet.
Art

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to derive Binary representation of Negative Numbers
 
Similar Threads
Converting negative numbers to binary?
confused by shift operators
shift
~ operator, explanation needed
>>> on negative numbers