A simple way to determine the two's compliment equivalent of a number is :
Start with the original, straight binary representation of the positive
number, scan to the left leaving all digits unchanged untill the first 1 is
encountered. Leave this one unchanged and invert the rest of the digits to
the left.crossing the radix point has no effect.
2d =0000 0000 0000 0010 -2d=1111 1111 1111 1110 (d is for decimal)
32767=0111 1111 1111 1111 -32767=1000 0000 0000 0001
not that the left most bit is sign (0 for positive 1 for negative)
The same result can be obtained by inverting the positive binary value and
then adding one:
+2d= 0000 0000 0000 0010
Invert: 1111 1111 1111 1101
Add 1 : 0000 0000 0000 0001
result: 1111 1111 1111 1110
> A simple way to determine the two's compliment equivalent of a number is :
> Start with the original, straight binary representation of the positive
> number, scan to the left leaving all digits unchanged untill the first 1 is
> encountered. Leave this one unchanged and invert the rest of the digits to
> the left.crossing the radix point has no effect.
-3 : 1101
When you say "simple", it may be reasonably simple for a person, but it's
much more complex for a computer than -n = ~n + 1. On the other hand, it is
useful to recognize that increment and decrement are really:
increment: toggle the rightmost zero and all the ones to its right
decrement: toggle the rightmost one and all the zeroes to its right
The definition you give for complementing corresponds with either (decrement
then flip all the bits) or (flip all the bits then increment).
Note that these definitions can be useful for operations such as "find the
rightmost one" [ lsb(n) = (n & ~(n-1)) ] or find the rightmost zero [ lsz(n)
= (~n & (n+1)) ] .
More... (looser matching)
- Last day of these posts
- In 1995
, 1996 only
- New search...