unsigned == signed ?

Discussion in 'C Programming' started by HH, Aug 6, 2004.

  1. HH

    HH Guest

    int main()
    {
    uint x = -1;
    int y = -1;
    if (x == y)
    printf("EQUAL");
    else
    printf("NOT EQUAL");
    }
    This code prints "EQUAL". Does this mean that bit comparison is done for the
    equality test? If it doesn't can you give me an example that shows bit
    comparison isn't done? I also wanted to know if we can assign a number to
    uint & int variables such that "unit var != int var". Thanks so much
     
    HH, Aug 6, 2004
    #1
    1. Advertising

  2. "HH" <> wrote in
    news:1cNQc.10804$:

    > int main()
    > {
    > uint x = -1;
    > int y = -1;
    > if (x == y)
    > printf("EQUAL");
    > else
    > printf("NOT EQUAL");
    > }
    > This code prints "EQUAL". Does this mean that bit comparison is done for
    > the equality test?


    I would say an unsigned comparison is being done. I believe that y will be
    promoted by C to unsigned int (which I assume uint is typedef'd to be)
    before doing the comparison so they should be equal. You should have
    gotten a warning that you are comparing signed with unsiged types - a clue
    to not do that.

    > If it doesn't can you give me an example that shows
    > bit comparison isn't done? I also wanted to know if we can assign a
    > number to uint & int variables such that "unit var != int var". Thanks
    > so much


    To compare values you must use reasonable types, there is no such thing as
    an unsiged -1 so how can you expect a meaningful comparison. You can
    always compare int to int and unsigned to unsigned but when comparing
    unsigned to signed it simply may not make sense.

    --
    - Mark ->
    --
     
    Mark A. Odell, Aug 6, 2004
    #2
    1. Advertising

  3. HH

    kyle york Guest

    Greetings,

    HH wrote:
    > int main()
    > {
    > uint x = -1;
    > int y = -1;
    > if (x == y)
    > printf("EQUAL");
    > else
    > printf("NOT EQUAL");
    > }
    > This code prints "EQUAL". Does this mean that bit comparison is done for the
    > equality test? If it doesn't can you give me an example that shows bit
    > comparison isn't done? I also wanted to know if we can assign a number to
    > uint & int variables such that "unit var != int var". Thanks so much


    Someone will likely complain about the lack of type uint in C as well as
    the undefined behaviour caused by not including stdio.h, as well as the
    possible lack of output due to not newline at the end of the printf().

    The answer, however, assuming uint to be equivalent to unsigned int, is
    that y is promoted to unsigned int before the comparison courtesy of
    section 6.3.1.8 : usual arithmetic conversion and these two values are
    indeed equal.


    --
    Kyle A. York
    Sr. Subordinate Grunt
    DSBU
     
    kyle york, Aug 6, 2004
    #3
  4. HH

    Eric Sosman Guest

    HH wrote:
    > int main()
    > {
    > uint x = -1;
    > int y = -1;
    > if (x == y)
    > printf("EQUAL");
    > else
    > printf("NOT EQUAL");
    > }
    > This code prints "EQUAL".


    Well, no: This code doesn't compile, because `uint'
    has not been declared. (There are other problems, too,
    but that's not the point.) I'll answer on the assumption
    that `uint' is a typedef'ed or #define'd alias for
    `unsigned int'.

    > Does this mean that bit comparison is done for the
    > equality test? If it doesn't can you give me an example that shows bit
    > comparison isn't done?


    No and no. C's comparison operators are all defined
    as working with the values of their operands, not the
    representations thereof. What you have discovered is
    that (1) when an `unsigned int' and an `int' are compared,
    the `int' value is first converted to `unsigned int', and
    (2) `(unsigned int)-1 == (unsigned int)-1'.

    > I also wanted to know if we can assign a number to
    > uint & int variables such that "unit var != int var". Thanks so much


    Assuming that `unit' is also an alias for `unsigned
    int', it is possible for this to happen -- but only under
    a rather "exotic" set of conditions. These conditions are
    permitted by the C language Standard, but never ("What,
    never?" "Well, hardly ever.") seen in practice:

    1) The number must be in the allowable range for an
    `unsigned int' but out of range for an `int', that
    is, `INT_MAX < number && number <= UINT_MAX'.

    2) The attempt to convert this too-large value to an
    `int' produces an implementation-defined result
    instead of raising an implementation-defined signal.

    3) The implementation-defined result is a valid `int'
    value with the interesting property that converting
    it to `unsigned int' does not yield the original
    number.

    This chain of events is unlikely, but permitted by the
    Standard -- converting a value from `unsigned int' to `int'
    and back again is required to be the identity operation only
    if the value is in the legitimate range of an `int'. If it's
    not, the Standard permits other things to happen. It's more
    a theoretical possibility than anything else, though.

    Here's the big question: Why do you care? What are you
    trying to do?

    --
     
    Eric Sosman, Aug 6, 2004
    #4
  5. HH

    someone else Guest

    "HH" <> wrote in message
    news:1cNQc.10804$...
    > int main()
    > {
    > uint x = -1;
    > int y = -1;
    > if (x == y)
    > printf("EQUAL");
    > else
    > printf("NOT EQUAL");
    > }
    > This code prints "EQUAL". Does this mean that bit comparison is done for

    the
    > equality test? If it doesn't can you give me an example that shows bit
    > comparison isn't done?

    no ! unsigned int, and signed int are very different the way they are
    represented in bits. therefore, when you to compare the 2 different types
    (like comparing apples and oranges) one of them has to be converted, in this
    case, the 'int' is converted to a 'uint', and becomes 0, since unsigned
    numbers cannot be less then zero.
    and besides, the == operator it *not* a bit comparison operator, it is a
    variable comparison operator, if you want to do bit comparison you use the
    bitwise operators, &,|,^,~. (and, or, exclusive or, not. respectively)

    >I also wanted to know if we can assign a number to
    > unit & int variables such that "unit var != int var". Thanks so much

    yes, if unit var = 1, and int var is any value other than 1 (-1, 0, -10, 10,
    100, whatever) then they will be 'not equal' if you use the operator '!='
    for example:

    uint x = 1;
    int y = -1;
    if (x == y)
    printf("EQUAL");
    else
    printf("NOT EQUAL");

    will print NOT EQUAL since y is converted to an unsigned int and becomes 0

    >
    >
     
    someone else, Aug 7, 2004
    #5
  6. HH

    Jack Klein Guest

    On 6 Aug 2004 15:52:38 GMT, "Mark A. Odell" <>
    wrote in comp.lang.c:

    > "HH" <> wrote in
    > news:1cNQc.10804$:
    >
    > > int main()
    > > {
    > > uint x = -1;
    > > int y = -1;
    > > if (x == y)
    > > printf("EQUAL");
    > > else
    > > printf("NOT EQUAL");
    > > }
    > > This code prints "EQUAL". Does this mean that bit comparison is done for
    > > the equality test?

    >
    > I would say an unsigned comparison is being done. I believe that y will be
    > promoted by C to unsigned int (which I assume uint is typedef'd to be)
    > before doing the comparison so they should be equal. You should have
    > gotten a warning that you are comparing signed with unsiged types - a clue
    > to not do that.
    >
    > > If it doesn't can you give me an example that shows
    > > bit comparison isn't done? I also wanted to know if we can assign a
    > > number to uint & int variables such that "unit var != int var". Thanks
    > > so much

    >
    > To compare values you must use reasonable types, there is no such thing as
    > an unsiged -1 so how can you expect a meaningful comparison. You can
    > always compare int to int and unsigned to unsigned but when comparing
    > unsigned to signed it simply may not make sense.


    Beg to disagree. There is indeed such a thing as unsigned -1. C
    defines both initializations and assignments in terms of values.

    Initializing any unsigned integer type with the value of -1, or
    assigning a value of -1 to any unsigned integer type, follows the
    usual unsigned rules of modulo wrap around.

    UTYPE x = -1;

    ....is guaranteed to produce the value UTYPE_MAX in x.

    Given:

    int si = -1;
    unsigned int ui = -1;

    ....then the comparison:

    if (si == ui)

    ....is guaranteed to be true. If the implementation uses a non-2's
    complement representation for negative signed integer types, the bit
    patterns of ui and si will be different, but the comparison is also
    based on *value*, not on bit representation.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 7, 2004
    #6
  7. HH

    Jack Klein Guest

    On Sat, 7 Aug 2004 08:27:30 +0800, "someone else"
    <zooloo@****.the.spammers.net> wrote in comp.lang.c:

    >
    > "HH" <> wrote in message
    > news:1cNQc.10804$...
    > > int main()
    > > {
    > > uint x = -1;
    > > int y = -1;
    > > if (x == y)
    > > printf("EQUAL");
    > > else
    > > printf("NOT EQUAL");
    > > }
    > > This code prints "EQUAL". Does this mean that bit comparison is done for

    > the
    > > equality test? If it doesn't can you give me an example that shows bit
    > > comparison isn't done?

    > no ! unsigned int, and signed int are very different the way they are
    > represented in bits. therefore, when you to compare the 2 different types
    > (like comparing apples and oranges) one of them has to be converted, in this
    > case, the 'int' is converted to a 'uint', and becomes 0, since unsigned
    > numbers cannot be less then zero.


    This is totally incorrect. Assigning or initializing any unsigned
    integer type with the value -1 is required and guaranteed to produce
    the maximum value of the unsigned type. Indeed no unsigned type can
    hold the value -1, but the conversion is completely and specifically
    defined.

    > and besides, the == operator it *not* a bit comparison operator, it is a
    > variable comparison operator, if you want to do bit comparison you use the
    > bitwise operators, &,|,^,~. (and, or, exclusive or, not. respectively)
    >
    > >I also wanted to know if we can assign a number to
    > > unit & int variables such that "unit var != int var". Thanks so much

    > yes, if unit var = 1, and int var is any value other than 1 (-1, 0, -10, 10,
    > 100, whatever) then they will be 'not equal' if you use the operator '!='
    > for example:


    You completely misunderstood this part of the OP's question.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 7, 2004
    #7
  8. HH

    someone else Guest

    "Jack Klein" <> wrote in message
    news:...
    > On Sat, 7 Aug 2004 08:27:30 +0800, "someone else"
    > <zooloo@****.the.spammers.net> wrote in comp.lang.c:
    >
    > >
    > > "HH" <> wrote in message
    > > news:1cNQc.10804$...
    > > > int main()
    > > > {
    > > > uint x = -1;
    > > > int y = -1;
    > > > if (x == y)
    > > > printf("EQUAL");
    > > > else
    > > > printf("NOT EQUAL");
    > > > }
    > > > This code prints "EQUAL". Does this mean that bit comparison is done

    for
    > > the
    > > > equality test? If it doesn't can you give me an example that shows bit
    > > > comparison isn't done?

    > > no ! unsigned int, and signed int are very different the way they are
    > > represented in bits. therefore, when you to compare the 2 different

    types
    > > (like comparing apples and oranges) one of them has to be converted, in

    this
    > > case, the 'int' is converted to a 'uint', and becomes 0, since unsigned
    > > numbers cannot be less then zero.

    >
    > This is totally incorrect. Assigning or initializing any unsigned
    > integer type with the value -1 is required and guaranteed to produce
    > the maximum value of the unsigned type. Indeed no unsigned type can
    > hold the value -1, but the conversion is completely and specifically
    > defined.

    true, my mistake.
    however the values are converted first. to prove it, try this
    long x = -1; /* (32 bit, signed) */
    unit y = -1; /* (16 bit, unsigned) */
    if (x == y) printf("EQUAL");
    will print EQUAL !

    >
    > > and besides, the == operator it *not* a bit comparison operator, it is a
    > > variable comparison operator, if you want to do bit comparison you use

    the
    > > bitwise operators, &,|,^,~. (and, or, exclusive or, not. respectively)
    > >
    > > >I also wanted to know if we can assign a number to
    > > > unit & int variables such that "unit var != int var". Thanks so much

    > > yes, if unit var = 1, and int var is any value other than 1 (-1, 0, -10,

    10,
    > > 100, whatever) then they will be 'not equal' if you use the operator

    '!='
    > > for example:

    >
    > You completely misunderstood this part of the OP's question.

    again, you are correct.
    but it's too bad that my personal beliefs do not allow me to respond... (I
    oppose rudeness)
    >
    > --
    > Jack Klein
    > Home: http://JK-Technology.Com
    > FAQs for
    > comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    > comp.lang.c++ http://www.parashift.com/c -faq-lite/
    > alt.comp.lang.learn.c-c++
    > http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    someone else, Aug 8, 2004
    #8
    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. Jeremy Pyle
    Replies:
    3
    Views:
    53,253
    Mike Treseler
    Jun 28, 2003
  2. Patrick

    signed to unsigned

    Patrick, Jun 7, 2004, in forum: VHDL
    Replies:
    1
    Views:
    5,643
    Ralf Hildebrandt
    Jun 7, 2004
  3. kyrpa83
    Replies:
    1
    Views:
    657
    kyrpa83
    Oct 17, 2007
  4. Rob1bureau
    Replies:
    1
    Views:
    837
    joris
    Feb 27, 2010
  5. pozz
    Replies:
    12
    Views:
    793
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page