1's complement and 2's complement

Discussion in 'C++' started by sarathy, Aug 1, 2006.

  1. sarathy

    sarathy Guest

    Hi all,
    I have a few doubts in the 1's and 2's complement
    representation. Generally negative numbers can be represented using
    either 1's complement or 2's complement representation.

    1's complement ---> reverse all the bits
    2's complement ---> reverse all the bits + 1

    i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
    But when a number and its complement are added the result must be a
    zero right ??
    But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
    Should'nt we be getting a zero as result ???

    2's complement of 2 ( 0000 0010 ) is -2 ( 1111 1110 )
    Adding we get , 0000 0010 + 1111 1110 = 0000 0000 ==> [ OK]

    Does this complement representation have anything to do with the C's ~
    [1's complement] operator ?
    Is this representation architecture dependent or compiler dependent ?

    Please clarify,

    Regards,
    Sarathy
    sarathy, Aug 1, 2006
    #1
    1. Advertising

  2. sarathy

    SM Ryan Guest

    "sarathy" <> wrote:
    # Hi all,
    # I have a few doubts in the 1's and 2's complement
    # representation. Generally negative numbers can be represented using
    # either 1's complement or 2's complement representation.
    #
    # 1's complement ---> reverse all the bits
    # 2's complement ---> reverse all the bits + 1
    #
    # i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
    # But when a number and its complement are added the result must be a
    # zero right ??
    # But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]

    On a ones complement machine, ~0 is 0, called a negative zero.
    Some CPUs convert -0 to +0, some don't. -0 = +0, but also
    sometimes -0 < +0.

    # Does this complement representation have anything to do with the C's ~
    # [1's complement] operator ?

    On ones complement CPUs, -x = ~x. Whether this was signficant when C
    was first created, you would have to ask Ritchie.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    So....that would make Bethany part black?
    SM Ryan, Aug 1, 2006
    #2
    1. Advertising

  3. sarathy

    red floyd Guest

    sarathy wrote:
    > Hi all,
    > I have a few doubts in the 1's and 2's complement
    > representation. Generally negative numbers can be represented using
    > either 1's complement or 2's complement representation.
    >
    > 1's complement ---> reverse all the bits
    > 2's complement ---> reverse all the bits + 1
    >
    > i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
    > But when a number and its complement are added the result must be a
    > zero right ??
    > But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
    > Should'nt we be getting a zero as result ???


    In a pure 1's complement notation, you have the concept of "minus zero",
    which is the ones complement of 0.

    So your result is "minus zero".
    red floyd, Aug 1, 2006
    #3
  4. sarathy

    Roy Smith Guest

    In article <>,
    "sarathy" <> wrote:

    > Hi all,
    > I have a few doubts in the 1's and 2's complement
    > representation. Generally negative numbers can be represented using
    > either 1's complement or 2's complement representation.
    >
    > 1's complement ---> reverse all the bits
    > 2's complement ---> reverse all the bits + 1
    >
    > i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
    > But when a number and its complement are added the result must be a
    > zero right ??
    > But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
    > Should'nt we be getting a zero as result ???


    You did. In 1's complement, there is no unique representation for zero.
    All 0's and all 1's are both equal to zero.

    > Does this complement representation have anything to do with the C's ~
    > [1's complement] operator ?


    Not really

    > Is this representation architecture dependent or compiler dependent ?


    Whether you are doing 1's complement or 2's complement math depends on the
    underlying hardware. That being said, I haven't seen a 1's complement
    machine in a couple of eons. It's pretty much an obsolete concept as far
    as hardware design goes.
    Roy Smith, Aug 1, 2006
    #4
  5. sarathy

    Bill Pursell Guest

    Roy Smith wrote:
    > In article <>,
    > "sarathy" <> wrote:


    > > 1's complement ---> reverse all the bits
    > > 2's complement ---> reverse all the bits + 1
    > >
    > > i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
    > > But when a number and its complement are added the result must be a
    > > zero right ??
    > > But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
    > > Should'nt we be getting a zero as result ???

    >
    > You did. In 1's complement, there is no unique representation for zero.
    > All 0's and all 1's are both equal to zero.


    No, in 8-bit ones complement, zero is represented as either
    0x00 or 0x80. 0xff is -127.

    The problem is that addition with one's complement is
    not the same as addition with 2's complement. To
    add two numbers, you have to perform different operations
    depending on the signedness of the numbers, and that
    is why 2's complement is preferred.
    Bill Pursell, Aug 1, 2006
    #5
  6. sarathy

    Michael Mair Guest

    Bill Pursell schrieb:
    > Roy Smith wrote:
    >>In article <>,
    >> "sarathy" <> wrote:

    >
    >>>1's complement ---> reverse all the bits
    >>>2's complement ---> reverse all the bits + 1
    >>>
    >>>i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
    >>>But when a number and its complement are added the result must be a
    >>>zero right ??
    >>>But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
    >>>Should'nt we be getting a zero as result ???

    >>
    >>You did. In 1's complement, there is no unique representation for zero.
    >>All 0's and all 1's are both equal to zero.

    >
    > No, in 8-bit ones complement, zero is represented as either
    > 0x00 or 0x80. 0xff is -127.


    8-bit ones complement? You mean sign and magnitude.

    There is only one kind of ones complement for C.

    C99, 62.6.2#2: "
    — the corresponding value with sign bit 0 is negated (sign and magnitude);
    — the sign bit has the value -(2N) (two’s complement);
    — the sign bit has the value -(2N - 1) (one’s complement).
    "

    > The problem is that addition with one's complement is
    > not the same as addition with 2's complement. To
    > add two numbers, you have to perform different operations
    > depending on the signedness of the numbers, and that
    > is why 2's complement is preferred.


    And one's complement and sign-magnitude have the advantage
    of symmetric value range and others. There have been enough
    threads on this.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Aug 1, 2006
    #6
  7. sarathy

    sarathy Guest

    Hi,
    I guess -0 ==> 1111 1111 is correct in 1's complement notation.
    -0 ==> 1000 0000 is in signed magnitude notation.

    Please verify and revert back in case.

    Rgrds,
    Sarathy

    Bill Pursell wrote:
    > Roy Smith wrote:
    > > In article <>,
    > > "sarathy" <> wrote:

    >
    > > > 1's complement ---> reverse all the bits
    > > > 2's complement ---> reverse all the bits + 1
    > > >
    > > > i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
    > > > But when a number and its complement are added the result must be a
    > > > zero right ??
    > > > But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
    > > > Should'nt we be getting a zero as result ???

    > >
    > > You did. In 1's complement, there is no unique representation for zero.
    > > All 0's and all 1's are both equal to zero.

    >
    > No, in 8-bit ones complement, zero is represented as either
    > 0x00 or 0x80. 0xff is -127.
    >
    > The problem is that addition with one's complement is
    > not the same as addition with 2's complement. To
    > add two numbers, you have to perform different operations
    > depending on the signedness of the numbers, and that
    > is why 2's complement is preferred.
    sarathy, Aug 1, 2006
    #7
  8. sarathy

    Bill Pursell Guest

    Michael Mair wrote:
    > Bill Pursell schrieb:
    > > Roy Smith wrote:
    > >>In article <>,
    > >> "sarathy" <> wrote:

    > >
    > >>>1's complement ---> reverse all the bits
    > >>>2's complement ---> reverse all the bits + 1
    > >>>
    > >>>i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 )
    > >>>But when a number and its complement are added the result must be a
    > >>>zero right ??
    > >>>But in this case 0000 0010 + 1111 1101 = 1111 1111 ==> [ ?? ]
    > >>>Should'nt we be getting a zero as result ???
    > >>
    > >>You did. In 1's complement, there is no unique representation for zero.
    > >>All 0's and all 1's are both equal to zero.

    > >
    > > No, in 8-bit ones complement, zero is represented as either
    > > 0x00 or 0x80. 0xff is -127.

    >
    > 8-bit ones complement? You mean sign and magnitude.


    Oops. Of course.

    > of symmetric value range and others. There have been enough
    > threads on this.


    Agreed!!

    --
    Bill
    Bill Pursell, Aug 1, 2006
    #8
  9. sarathy posted:

    > Please verify and revert back in case.



    *Cringe*

    I'd love to bludgeon to death the next person I hear utter that phrase.

    --

    Frederick Gotham
    Frederick Gotham, Aug 1, 2006
    #9
  10. Frederick Gotham said:

    > sarathy posted:
    >
    >> Please verify and revert back in case.

    >
    >
    > *Cringe*
    >
    > I'd love to bludgeon to death the next person I hear utter that phrase.


    Are you sure about that? Please verify and revert back in case.



    (And now if you'll excuse me, I have a plane to catch. Or a starship. Or
    something... TAXI!)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Aug 1, 2006
    #10
  11. Richard Heathfield posted:

    >>> Please verify and revert back in case.


    >> *Cringe*
    >>
    >> I'd love to bludgeon to death the next person I hear utter that phrase.


    > Are you sure about that? Please verify and revert back in case.
    >
    >
    >
    > (And now if you'll excuse me, I have a plane to catch. Or a starship. Or
    > something... TAXI!)



    That phrase brings back horrible memories of working in an office full of
    social retards. Never again.

    --

    Frederick Gotham
    Frederick Gotham, Aug 1, 2006
    #11
  12. =?iso-8859-1?q?Kirit_S=E6lensminde?=, Aug 1, 2006
    #12
  13. Roy Smith wrote:
    It's pretty much an obsolete concept as far as hardware design goes.

    Not quite, many DSP-oriented CPU's use 1's complement arithmetic.

    The advantage is, in a chain calculation, the negates and carries can
    be computed separately and andded back at the end. With two's
    complement the "add one" has to be done on each negate.
    Ancient_Hacker, Aug 1, 2006
    #13
  14. "Kirit Sælensminde" <> writes:
    > Roy Smith wrote:
    >> That being said, I haven't seen a 1's complement
    >> machine in a couple of eons. It's pretty much an obsolete concept as far
    >> as hardware design goes.

    >
    > Except of course as part of the format for IEEE floating point numbers
    > (float, double etc.).
    >
    > http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_14/CH14-1.html#HEADING1-52


    Actually, I think it's sign-and-magnitude, not one's-complement.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 1, 2006
    #14
  15. In article <> "=?iso-8859-1?q?Kirit_S=E6lensminde?=" <> writes:
    >
    > Roy Smith wrote:
    > > That being said, I haven't seen a 1's complement
    > > machine in a couple of eons. It's pretty much an obsolete concept as far
    > > as hardware design goes.

    >
    > Except of course as part of the format for IEEE floating point numbers
    > (float, double etc.).
    >
    > http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_14/CH14-1.html#HEADING1-52


    I would not trust a book by an author who does not know the difference
    between 1-s complement and sign-magnitude. The last machine I had
    access to that used 1-s complement was the CDC Cyber 750, and the
    successor in 750 mode (both for int and for float).
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Aug 2, 2006
    #15
  16. Frederick Gotham wrote:
    > sarathy posted:
    >
    > > Please verify and revert back in case.

    >
    >
    > *Cringe*
    >
    > I'd love to bludgeon to death the next person I hear utter that phrase.


    I've never come across it before; what does it mean? Am I allowed to
    revert to any previous condition, or is a particular one implied?
    J. J. Farrell, Aug 2, 2006
    #16
  17. sarathy

    Joe Wright Guest

    Dik T. Winter wrote:
    > In article <> "=?iso-8859-1?q?Kirit_S=E6lensminde?=" <> writes:
    > >
    > > Roy Smith wrote:
    > > > That being said, I haven't seen a 1's complement
    > > > machine in a couple of eons. It's pretty much an obsolete concept as far
    > > > as hardware design goes.

    > >
    > > Except of course as part of the format for IEEE floating point numbers
    > > (float, double etc.).
    > >
    > > http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_14/CH14-1.html#HEADING1-52

    >
    > I would not trust a book by an author who does not know the difference
    > between 1-s complement and sign-magnitude. The last machine I had
    > access to that used 1-s complement was the CDC Cyber 750, and the
    > successor in 750 mode (both for int and for float).


    Nobody doubts there were 1's complement iron, but when? The last CDC
    machine I saw was the 160A in 1962 and I have no idea of its arithmetic
    mode. In 1963 I learned the Philco 212/2000 system which was 2's
    complement. Every machine I've seen since then is 2's complement for
    integer arithmetic. That's 43 years. But I haven't seen them all.

    What was the last 1's complement machine and when was it last produced?

    I have never seen 'signed magnitude' integers on any machine.

    Of course, IEEE floating point is signed magnitude. FP is not the issue.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Aug 2, 2006
    #17
  18. Keith Thompson wrote:
    > Actually, I think it's sign-and-magnitude, not one's-complement.


    Whoops. Fair enough.


    K
    =?iso-8859-1?q?Kirit_S=E6lensminde?=, Aug 2, 2006
    #18
  19. sarathy

    Roy Smith Guest

    In article <>,
    Joe Wright <> wrote:

    > What was the last 1's complement machine and when was it last produced?


    Wikipedia (http://en.wikipedia.org/wiki/One's_complement) claims "the
    PDP-1 and UNIVAC 1100/2200 series, among many others, used one's-complement
    arithmetic."
    Roy Smith, Aug 2, 2006
    #19
  20. sarathy

    Richard Bos Guest

    "J. J. Farrell" <> wrote:

    > Frederick Gotham wrote:
    > > sarathy posted:
    > >
    > > > Please verify and revert back in case.

    > >
    > > *Cringe*
    > >
    > > I'd love to bludgeon to death the next person I hear utter that phrase.

    >
    > I've never come across it before; what does it mean?


    It's managementspeak. The presence of any meaning is purely optional.

    Richard
    Richard Bos, Aug 2, 2006
    #20
    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. Kamlesh
    Replies:
    0
    Views:
    947
    Kamlesh
    Apr 21, 2005
  2. Mantorok Redgormor

    sign magnitude, ones complement, two's complement

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

    1's complement and 2's complement

    sarathy, Aug 1, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,172
    Bo Persson
    Aug 2, 2006
  4. Roberto Waltman

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

    Roberto Waltman, Jun 9, 2011, in forum: C Programming
    Replies:
    4
    Views:
    1,339
    Michael Press
    Jun 14, 2011
  5. Andrew Savige
    Replies:
    2
    Views:
    223
    Andrew Savige
    Mar 26, 2009
Loading...

Share This Page