posted 14 years ago
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 ]
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors