[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

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

Roberto Waltman

6/9/2011 4:27:00 PM

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 ]
5 Answers

blp

6/9/2011 4:33:00 PM

0

Roberto Waltman <usenet@rwaltman.com> 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

6/9/2011 4:34:00 PM

0

Roberto Waltman <usenet@rwaltman.com> 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) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Joe Pfeiffer

6/9/2011 8:04:00 PM

0

Keith Thompson <kst-u@mib.org> writes:

> Roberto Waltman <usenet@rwaltman.com> 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

6/13/2011 11:26:00 PM

0

In article <qps1v6t27904hhdgd0916hnca7t676q168@4ax.com>,
Roberto Waltman <usenet@rwaltman.com> 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

China Blue Veins

6/13/2011 11:47:00 PM

0

In article <rubrum-25AA60.16260013062011@news.albasani.net>,
Michael Press <rubrum@pacbell.net> wrote:

> In article <qps1v6t27904hhdgd0916hnca7t676q168@4ax.com>,
> Roberto Waltman <usenet@rwaltman.com> 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.

Now multiply two numbers.

--
I remember finding out about you, | I survived XYZZY-Day.
Everyday my mind is all around you,| I'm whoever you want me to be.
Looking out from my lonely room |Annoying Usenet one post at a time.
Day after day. | At least I can stay in character.