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

Discussion in 'C Programming' started by Roberto Waltman, Jun 9, 2011.

1. ### Roberto WaltmanGuest

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

[ Please reply to the group.
Return address is invalid ]

Roberto Waltman, Jun 9, 2011

2. ### Ben PfaffGuest

Roberto Waltman <> 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_

Ben Pfaff, Jun 9, 2011

3. ### Keith ThompsonGuest

Roberto Waltman <> 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.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jun 9, 2011
4. ### Joe PfeifferGuest

Keith Thompson <> writes:

> Roberto Waltman <> 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.

Joe Pfeiffer, Jun 9, 2011
5. ### Michael PressGuest

In article <>,
Roberto Waltman <> 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.)

Bignum packages. Here, you answer your own question.
Design routines to add two bignums.
One for signed-magnitude representation,
one for 2's complement representation.

--
Michael Press

Michael Press, Jun 14, 2011

## Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.