Velocity Reviews > 2's complement vs. 1's complement vs. ...

# 2's complement vs. 1's complement vs. ...

Roberto Waltman
Guest
Posts: n/a

 06-09-2011
Peter Nilsson wrote:
> Keith Thompson wrote:

>> A 2's-complement representation makes some operations easier,

>And some harder.

Could you expand this? What operations are easier using other
representations? (Other than BCD, etc. for financial applications.)
--
Roberto Waltman

Ben Pfaff
Guest
Posts: n/a

 06-09-2011
Roberto Waltman <(E-Mail Removed)> writes:

> Peter Nilsson wrote:
>> Keith Thompson wrote:

>
>>> A 2's-complement representation makes some operations easier,

>
>>And some harder.

>
> Could you expand this? What operations are easier using other
> representations? (Other than BCD, etc. for financial applications.)

I suspect (without looking it up) that multiplication and
division with negative numbers is harder in two's complement than
in sign-magnitude.
--
"Programmers have the right to be ignorant of many details of your code
and still make reasonable changes."
--Kernighan and Plauger, _Software Tools_

Keith Thompson
Guest
Posts: n/a

 06-09-2011
Roberto Waltman <(E-Mail Removed)> writes:
>> Keith Thompson wrote:
>>> A 2's-complement representation makes some operations easier,

>
>>And some harder.

>
> Could you expand this? What operations are easier using other
> representations? (Other than BCD, etc. for financial applications.)

The most obvious example is negation. In sign and magnitude, you
just invert the sign bit. In ones' complement, you invert every bit.
In two's complement, you invert every bit and then increment (which
can toggle every bit again).

This assumes that he difficulty of an operation is measured by the
number of bits that are affected, which probably isn't the case.

(Note the correct placement of the apostrophes: "two's complement"
and "ones' complement".)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Joe Pfeiffer
Guest
Posts: n/a

 06-09-2011
Keith Thompson <(E-Mail Removed)> writes:

> Roberto Waltman <(E-Mail Removed)> writes:
>>> Keith Thompson wrote:
>>>> A 2's-complement representation makes some operations easier,

>>
>>>And some harder.

>>
>> Could you expand this? What operations are easier using other
>> representations? (Other than BCD, etc. for financial applications.)

>
> The most obvious example is negation. In sign and magnitude, you
> just invert the sign bit. In ones' complement, you invert every bit.
> In two's complement, you invert every bit and then increment (which
> can toggle every bit again).

A 2's complement carry-lookahead adder takes half the carry logic of a
1's complement (because the 1's complement has a wrap-around carry).

A sign-magnitude adder is a mess, since you have to compare the signs on
the inputs to decide whether you're going to add vs. subtract, and
compare magnitudes to decide which one you subtract from the other.

By the time you've got the hardware on hand to both add and subtract,
being able to invert a number doesn't take any new hardware to speak of.

> This assumes that he difficulty of an operation is measured by the
> number of bits that are affected, which probably isn't the case.

Michael Press
Guest
Posts: n/a

 06-13-2011
In article <(E-Mail Removed)>,
Roberto Waltman <(E-Mail Removed)> wrote:

> Peter Nilsson wrote:
> > Keith Thompson wrote:

>
> >> A 2's-complement representation makes some operations easier,

>
> >And some harder.

>
> Could you expand this? What operations are easier using other
> representations? (Other than BCD, etc. for financial applications.)

Design routines to add two bignums.
One for signed-magnitude representation,
one for 2's complement representation.

--
Michael Press