one's compliment operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deepak

    one's compliment operator


    Hi,

    when I execute following program which find's one's compliment of 2,
    I'm getting -3 where
    I was expecting -2. Is there anything fundamentally wrong in my
    understanding?


    main()
    {
    int a = 2;

    printf("%d\n", ~a);
    }

    O/P:
    -3

    Thanks,
    Deepak
  • Gordon Burditt

    #2
    Re: one's compliment operator

    >when I execute following program which find's one's compliment of 2,
    >I'm getting -3 where
    That output is correct, for a two's complement machine.
    >I was expecting -2.
    Take that bit pattern over to a one's complement machine, and it
    will print -2 there. The same bit pattern on a two's complement
    machine is -3.
    >Is there anything fundamentally wrong in my
    >understandin g?
    The one's complement is one less than the two's complement.
    On a two's complement machine (close to all machines are,
    especially modern ones), the two's complement of N is
    displayed as -N, and the one's complement of N is -N-1.

    For example, the two's complement of 0 is 0, and the
    one's complement of 0 is -1.
    >main()
    >{
    int a = 2;
    >
    printf("%d\n", ~a);
    >}
    >
    >O/P:
    >-3

    Comment

    • deepak

      #3
      Re: one's compliment operator

      On Jul 16, 12:37 pm, gordonb.hp...@b urditt.org (Gordon Burditt) wrote:
      when I execute following program which find's one's compliment of 2,
      I'm getting -3 where
      >
      That output is correct, for a two's complement machine.
      >
      I was expecting -2.
      >
      Take that bit pattern over to a one's complement machine, and it
      will print -2 there. The same bit pattern on a two's complement
      machine is -3.
      >
      Is there anything fundamentally wrong in my
      understanding?
      >
      The one's complement is one less than the two's complement.
      On a two's complement machine (close to all machines are,
      especially modern ones), the two's complement of N is
      displayed as -N, and the one's complement of N is -N-1.
      >
      What machine is going to gain by changing it to 2's complement from
      1's complement?
      For example, the two's complement of 0 is 0, and the
      one's complement of 0 is -1.
      >
      main()
      {
      int a = 2;
      >
      printf("%d\n", ~a);
      }
      >
      O/P:
      -3

      Comment

      • Martin Ambuhl

        #4
        Re: one's compliment operator

        deepak wrote:
        Hi,
        >
        when I execute following program which find's one's compliment of 2,
        I'm getting -3 where
        I was expecting -2. Is there anything fundamentally wrong in my
        understanding?
        Yes, there is.
        >
        >
        main()
        {
        int a = 2;
        >
        printf("%d\n", ~a);
        }
        >
        O/P:
        -3
        Examine closely the output of the following code and try to see what's
        happening. Note that the code below, unlike yours, properly specifies
        the headers included, properly indicates the return type for main, and
        properly returns a value from main (and one with a defined meaning).

        #include <stdio.h>
        #include <string.h>

        int main(void)
        {
        /* There are purposeful inefficiencies below for the sake of clarity */
        unsigned int x;
        signed int y, y2;
        for (y = 10; y >= -10; y--) {
        memcpy(&x, &y, sizeof x);
        printf("y = %3d, x = %#0o | ", y, x);
        x = ~x;
        memcpy(&y2, &x, sizeof x);
        printf("~x = %#0o, y2 = %d\n", x, y2);
        }
        return 0;
        }


        y = 10, x = 012 | ~x = 037777777765, y2 = -11
        y = 9, x = 011 | ~x = 037777777766, y2 = -10
        y = 8, x = 010 | ~x = 037777777767, y2 = -9
        y = 7, x = 07 | ~x = 037777777770, y2 = -8
        y = 6, x = 06 | ~x = 037777777771, y2 = -7
        y = 5, x = 05 | ~x = 037777777772, y2 = -6
        y = 4, x = 04 | ~x = 037777777773, y2 = -5
        y = 3, x = 03 | ~x = 037777777774, y2 = -4
        y = 2, x = 02 | ~x = 037777777775, y2 = -3
        y = 1, x = 01 | ~x = 037777777776, y2 = -2
        y = 0, x = 0 | ~x = 037777777777, y2 = -1
        y = -1, x = 037777777777 | ~x = 0, y2 = 0
        y = -2, x = 037777777776 | ~x = 01, y2 = 1
        y = -3, x = 037777777775 | ~x = 02, y2 = 2
        y = -4, x = 037777777774 | ~x = 03, y2 = 3
        y = -5, x = 037777777773 | ~x = 04, y2 = 4
        y = -6, x = 037777777772 | ~x = 05, y2 = 5
        y = -7, x = 037777777771 | ~x = 06, y2 = 6
        y = -8, x = 037777777770 | ~x = 07, y2 = 7
        y = -9, x = 037777777767 | ~x = 010, y2 = 8
        y = -10, x = 037777777766 | ~x = 011, y2 = 9

        Comment

        • osmium

          #5
          Re: one's compliment operator

          "deepak" writes:
          On Jul 16, 12:37 pm, gordonb.hp...@b urditt.org (Gordon Burditt) wrote:
          >when I execute following program which find's one's compliment of 2,
          >I'm getting -3 where
          >>
          >That output is correct, for a two's complement machine.
          >>
          >I was expecting -2.
          >>
          >Take that bit pattern over to a one's complement machine, and it
          >will print -2 there. The same bit pattern on a two's complement
          >machine is -3.
          >>
          >Is there anything fundamentally wrong in my
          >understandin g?
          >>
          >The one's complement is one less than the two's complement.
          >On a two's complement machine (close to all machines are,
          >especially modern ones), the two's complement of N is
          >displayed as -N, and the one's complement of N is -N-1.
          >>
          What machine is going to gain by changing it to 2's complement from
          1's complement?
          The first step is to identify what "it" is. Is "it" a computer? The design
          of a computer? A number? All of the above? None of the above?


          Comment

          • Keith Thompson

            #6
            Re: one's compliment operator

            deepak <deepakpjose@gm ail.comwrites:
            [...]
            What machine is going to gain by changing it to 2's complement from
            1's complement?
            [...]

            I'm having trouble understanding your question, but I'll try to answer
            it anyway.

            Representing unsigned integers is straightforward : each bit represents
            a power of two. (There are complications, but we'll ignore them for
            now). Representing signed integers is a bit more tricky.

            There are three major ways to represent signed integers:

            sign-and-magnitude simply reserves a bit to indicate the sign; this
            bit is set to 0 for positive numbers, 1 for negative numbers. To
            negate a number, just flip the sign bit.

            In ones' complement, you negate a number by flipping *all* the bits.

            In two's complement, you negate a number by flipping all the bits and
            then adding 1.

            It turns out that two's complement has considerable advantages over
            the other representations . For one thing, it doesn't have two
            different representations for zero; if you negate 0, you just get 0
            again. The other representations both have distinct representations
            for +0 and -0, which means you have to decide how to deal with them.
            (Are +0 and -0 equal? If so, you can't compare two numbers just by
            comparing all the bits.) Also, if you ignore overflow/wraparound,
            many signed and unsigned operations are identical.

            For this and other reasons, almost all machines these days use two's
            complement for signed integers; the other representations aren't quite
            dead, but they're rare.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <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"

            Comment

            • Richard Tobin

              #7
              Re: one's compliment operator

              In article <lnhcapzvzd.fsf @nuthaus.mib.or g>,
              Keith Thompson <kst-u@mib.orgwrote:
              >It turns out that two's complement has considerable advantages over
              >the other representations . For one thing, it doesn't have two
              >different representations for zero; if you negate 0, you just get 0
              >again. The other representations both have distinct representations
              >for +0 and -0, which means you have to decide how to deal with them.
              >(Are +0 and -0 equal? If so, you can't compare two numbers just by
              >comparing all the bits.) Also, if you ignore overflow/wraparound,
              >many signed and unsigned operations are identical.
              The most compelling reason is that it's just what you get if you apply
              the usual algorithms for positive numbers even when the result might
              be negative. You don't need to do anything special about negative
              numbers at all.

              Try it in base ten: subtract one from zero. 0 - 1 you can't do, so borrow
              10. 10 - 1 is 9. Continue for however many digits you're using, and you'll
              find 0 - 1 is 999...999. You're using ten's complement.

              So to turn Keith's last sentence around, it's not that you find that
              signed and unsigned operations are identical, it's that when you apply
              operations without considering sign you find that you have invented a
              consistent way of representing negative numbers.

              -- Richard
              --
              Please remember to mention me / in tapes you leave behind.

              Comment

              • jameskuyper@verizon.net

                #8
                Re: one's compliment operator

                deepak wrote:
                Hi,
                >
                when I execute following program which find's one's compliment of 2,
                I'm getting -3 where
                I was expecting -2. Is there anything fundamentally wrong in my
                understanding?
                >
                >
                main()
                {
                int a = 2;
                >
                printf("%d\n", ~a);
                }
                >
                O/P:
                -3
                What's fundamentally wrong with your understanding is your expectation
                that the '~' operator would give you the ones' complEment of it's
                operand. That's not how it's defined. The correct definition is in
                6.5.3.3p4:

                "The result of the ~ operator is the bitwise complement of its
                (promoted) operand (that is,
                each bit in the result is set if and only if the corresponding bit in
                the converted operand is
                not set)."

                That is the same as the ones' complement only when the promoted type
                of the operand is represented on that machine using one's complement
                notation. Since machines using one's complement notation are extremely
                rare nowadays, you're far more likely to generate the twos' complement
                when you apply the '~' operator.

                Comment

                • Keith Thompson

                  #9
                  Re: one's compliment operator

                  jameskuyper@ver izon.net writes:
                  deepak wrote:
                  >when I execute following program which find's one's compliment of 2,
                  >I'm getting -3 where
                  >I was expecting -2. Is there anything fundamentally wrong in my
                  >understandin g?
                  >>
                  >>
                  >main()
                  >{
                  > int a = 2;
                  >>
                  > printf("%d\n", ~a);
                  >}
                  >>
                  >O/P:
                  >-3
                  >
                  What's fundamentally wrong with your understanding is your expectation
                  that the '~' operator would give you the ones' complEment of it's
                  operand. That's not how it's defined. The correct definition is in
                  6.5.3.3p4:
                  >
                  "The result of the ~ operator is the bitwise complement of its
                  (promoted) operand (that is,
                  each bit in the result is set if and only if the corresponding bit in
                  the converted operand is
                  not set)."
                  >
                  That is the same as the ones' complement only when the promoted type
                  of the operand is represented on that machine using one's complement
                  notation. Since machines using one's complement notation are extremely
                  rare nowadays, you're far more likely to generate the twos' complement
                  when you apply the '~' operator.
                  My understanding of what "the ones' complement" of a number means
                  differs from yours.

                  Here's how I'd say it:

                  The ~ operator does yield the ones' complement of its operand. This
                  won't be the *negated* value of the operand unless the system happens
                  to use a ones' complement representation for signed integers.

                  On a two's complement machine, the ~ operator still yields the ones'
                  complement of its operand. If it yielded the two's complement, as you
                  said above, it would be a negation operator; it isn't.

                  Though I probably wouldn't refer to ones' complement as an operation.
                  The operation in question is the bitwise complement. "Ones'
                  complement" usually refers to the particular representation that uses
                  the bitwise complement operator for arithmetic negation.

                  If you want the negated value of something just use the unary "-"
                  operator.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <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"

                  Comment

                  • jameskuyper@verizon.net

                    #10
                    Re: one's compliment operator

                    Keith Thompson wrote:
                    jameskuyper@ver izon.net writes:
                    deepak wrote:
                    when I execute following program which find's one's compliment of 2,
                    I'm getting -3 where
                    I was expecting -2. Is there anything fundamentally wrong in my
                    understanding?
                    >
                    >
                    main()
                    {
                    int a = 2;
                    >
                    printf("%d\n", ~a);
                    }
                    >
                    O/P:
                    -3
                    What's fundamentally wrong with your understanding is your expectation
                    that the '~' operator would give you the ones' complEment of it's
                    operand. That's not how it's defined. The correct definition is in
                    6.5.3.3p4:

                    "The result of the ~ operator is the bitwise complement of its
                    (promoted) operand (that is,
                    each bit in the result is set if and only if the corresponding bit in
                    the converted operand is
                    not set)."

                    That is the same as the ones' complement only when the promoted type
                    of the operand is represented on that machine using one's complement
                    notation. Since machines using one's complement notation are extremely
                    rare nowadays, you're far more likely to generate the twos' complement
                    when you apply the '~' operator.
                    >
                    My understanding of what "the ones' complement" of a number means
                    differs from yours.
                    To be honest, I have no such understanding of my own; I was making a
                    guess as to what the term meant to deepak. I'm familiar with the term
                    only as the name for a method of representing numbers, not as the name
                    for an operation that could be performed on them.

                    The one thing that I was clear about is that what he called the ones'
                    complement of a number differs from what the standard requires for the
                    result of applying the '~' operator on a value whose promoted type has
                    a twos' complement representation.

                    ....
                    Though I probably wouldn't refer to ones' complement as an operation.
                    The operation in question is the bitwise complement. "Ones'
                    complement" usually refers to the particular representation that uses
                    the bitwise complement operator for arithmetic negation.
                    So we're pretty much in agreement.

                    Comment

                    • Gordon Burditt

                      #11
                      Re: one's compliment operator

                      >when I execute following program which find's one's compliment of 2,
                      >I'm getting -3 where
                      >>
                      >That output is correct, for a two's complement machine.
                      >>
                      >I was expecting -2.
                      >>
                      >Take that bit pattern over to a one's complement machine, and it
                      >will print -2 there. The same bit pattern on a two's complement
                      >machine is -3.
                      >>
                      >Is there anything fundamentally wrong in my
                      >understandin g?
                      >>
                      >The one's complement is one less than the two's complement.
                      >On a two's complement machine (close to all machines are,
                      >especially modern ones), the two's complement of N is
                      >displayed as -N, and the one's complement of N is -N-1.
                      >>
                      >What machine is going to gain by changing it to 2's complement from
                      >1's complement?
                      It doesn't *change* anything. It interprets it as though it were
                      2's complement (or whatever the native complement is). If you
                      printf() something using a signed integer conversion, it interprets
                      it as though it were the native complement (in this case giving
                      -3). If you printf() the same thing using an unsigned integer
                      conversion, it interprets it as though it were unsigned (so it might
                      print 65533 on a machine with 16-bit ints). If you printf() something
                      using a %lf conversion, it interprets it as though it were a double,
                      which may (and likely will) have a drastically different value than
                      a long integer you passed in, even if there wasn't an issue of
                      doubles and longs possibly being different sizes.

                      Note that there's no little tag carried with the number saying "this
                      is in 1's complement". What you get is raw bits. C doesn't have
                      runtime-variable types, and in any case, there's no type for
                      explicitly-1's-complement integers.

                      The primary use of the complement operator is not to calculate stuff
                      in 1's complement. The primary use is as a bitwise NOT operator,
                      and it's commonly used along with the & and | bitwise operators.

                      Comment

                      Working...