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

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

  1. 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
    #1
    1. Advertising

  2. Roberto Waltman

    Ben Pfaff Guest

    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
    #2
    1. Advertising

  3. 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
    #3
  4. Roberto Waltman

    Joe Pfeiffer Guest

    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
    #4
  5. 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
    #5
    1. Advertising

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.
Similar Threads
  1. Troels Smit

    Conversion 1QN -> 2'Complement

    Troels Smit, Jun 30, 2003, in forum: VHDL
    Replies:
    1
    Views:
    4,181
    Troels Smit
    Jul 1, 2003
  2. nick
    Replies:
    3
    Views:
    40,527
    vsharma030
    Sep 12, 2012
  3. Mantorok Redgormor

    sign magnitude, ones complement, two's complement

    Mantorok Redgormor, Oct 5, 2003, in forum: C Programming
    Replies:
    8
    Views:
    8,616
    Glen Herrmannsfeldt
    Oct 8, 2003
  4. sarathy

    1's complement and 2's complement

    sarathy, Aug 1, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,203
    Bo Persson
    Aug 2, 2006
  5. sarathy
    Replies:
    22
    Views:
    2,360
    Bo Persson
    Aug 2, 2006
Loading...

Share This Page