Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

# Negative Value of Binary Complements

Greenhorn
Posts: 9
Hi,
Can anybody help me with the calculation of decimal from binary value of a negative number. I'll explain the problem i'm facing with an example :-

Suppose i have a decimal number char v = 41
Binary value of v = 101001
Form 1's complement of v or ~v = 010110 (inverted the bits)

This example is from Khalid Mughal (pg - 77 2nd Edition). According to the book, the decimal value of bitwise complement above ~v is -42. I'm not able to understand how this decimal value is calulated from the binary representation. If i go ahead and simply calculate the decimal from binary as:-
=0*2power(0)+1*2power(1)+1*2power(2)+0*2power(3)+1*2power(4)+0*2power(5)
= 0+2+4+0+16+0
=22

This value is not -42 where as the book says it should be -42. Can anybody help me with a way of calulating negative decimal values from such binaries.

Thanks and Best Regards,
Syed Rizvi
Deloitte Consulting.

author
Posts: 23879
142
To negate a number, you have to flip all the bits, and then add one.

Henry

Syed Rizvi
Greenhorn
Posts: 9
Thanks for the reply henry..but thats what exactly what i wanted to know..how do u convert the negative binary to negative decimal. As your reply suggest, adding 1 to the invert of 41 gives :-

= 1111 1111 1111 1111 1111 1111 1101 0111 (add 1)

Now how do i deduce from this that this indeed is the binary of -41 ?

Thanks Again.

Rizvi

Ranch Hand
Posts: 67
Hi,
1111 1111 1111 1111 1111 1111 1101 0111 (add 1)

Flip all bits si we get
0000 0000 0000 0000 0000 0000 0010 1000
0000 0000 0000 0000 0000 0000 0010 1001 (add 1)
Apply 2s power and we get 32+8+1 = 41.
Hope this helps.

Syed Rizvi
Greenhorn
Posts: 9
Thanks for the reply abhijit..but i guess u did not get my point. Please see my original post..that is the very first question i asked which henry answered...i'll just repeat again for clarity...

Suppose i have a decimal number char v = 41
Binary value of v = 101001
Form 1's complement of v or ~v = 010110 (inverted the bits)

Now my problem here is...how do i come to know the decimal value of ~v. The book that i'm referring to (Khalid Mughal) suggests that the decimal value of ~v is -42. So my question is...How do i deduce from a binary value of ~v (which is 010110) that its corresponding decimal value is indeed -42.

Hope i was clear enough to explain the problem....

Thanks again,
Rizvi

lowercase baba
Posts: 12871
62
First, you have an error in your logic. The binary value of v is NOT 101001. a char is 8 bits, so 41 is 00101001. those leading 0's make a difference when you invert it.

1's complement of 00101001 is 11010110.

if you want to know the decimal value of this, the first thing you do is look at the leftmost bit. if it's a 0, the value is positive. if it's a 1, the result is negative. remember this.

Now, if the value is positive, it's a pretty straight forward process.

if it's negative, you need to take the 2's complement. invert all the bits and add 1.

so, we get 00101001 from the inversion, then

00101010

from adding 1. this then converts to 42. and since we remembered our result is negative, -42.
[ January 08, 2006: Message edited by: fred rosenberger ]

Henry Wong
author
Posts: 23879
142
Or another way to view the issue... the technique to negate a number works both ways, positive to negative, and negative to positive. (kinda obvious for math, but may be lost since we are talking about bits)

Just look at the highest bit, it is the sign bit. If it is a one, negate the bits (invert plus one), calculate the value as previous, and that is the number of the negative value.

For this to work, you absolutely need to know the size of the variable, since short, int, and long have the sign bit at different locations. Furthermore, I believe char are unsigned, so it is moot.

Henry

Syed Rizvi
Greenhorn
Posts: 9
Thanks for the reply and help Fred and Henry...

Just one more thing from u fred..you said...

"so, we get 00101001 from the inversion, then

00101010

How do you arrive at binary 00101010 after adding 1 to binary 00101001 as you said above. 00101001 already has 1 as the right most bit, so how come adding 1 to this value makes it 00101010 ? Is there a shift of bit or something ?

Thanks again both of you

Syed Rizvi

Ranch Hand
Posts: 3271

Negative Numbers in Java - Two's Complement

Hex to Decimal and Back Again...

And, one more thing to keep in mind - this material is on the 1.4 exam, but is no longer included on the 1.5 exam.

Greenhorn
Posts: 6
Hi fred...

Just a small correction. Java Char is 2 bytes [16 bits] and not 8 bits.

Thanks

fred rosenberger
lowercase baba
Posts: 12871
62
ah, yes. it was early. sorry about that.

so the value would be

0000000000101001

negate and you get

1111111111010110

to figure out this value, we look at the left bit, and learn it's negative.

take the twos complement (invert and add 1).

when you add in binary, you can only have a 1 and a 0. so,
0 + 0 = 0
1 + 0 = 1
0 + 1 = 1
1 + 1 = 10

so above, when i add the righmost 1 and 1, i get 10. the 0 comes down, i carry the 1, and add it to the 0 to get 1.