comparison signed vs unsigned

Discussion in 'C++' started by Timothee Groleau, May 6, 2006.

  1. Hi all,

    When should I be worried about doing a comparison of signed vs unsigned
    ints? Could someone give me a example where such a comparison would lead to
    unwanted results?

    Thanks,
    Tim.
     
    Timothee Groleau, May 6, 2006
    #1
    1. Advertisements

  2. Timothee Groleau

    Rolf Magnus Guest

    #include <iostream>

    int main()
    {
    int a = -100;
    unsigned int b = 100;
    std::cout << a << " is "
    << (a < b ? "" : "not") << " less than " << b << '\n';
    }
     
    Rolf Magnus, May 6, 2006
    #2
    1. Advertisements

  3. Timothee Groleau

    Rolf Magnus Guest

    Another example:

    #include <iostream>

    int main()
    {
    int a = -1;
    unsigned int b = std::numeric_limits<unsigned int>::max();
    std::cout << a << " is " << (a == b ? "" : "not")
    << " equal to " << b << '\n';
    }
     
    Rolf Magnus, May 6, 2006
    #3
  4. Thanks Rolf!
     
    Timothee Groleau, May 6, 2006
    #4
  5. What's the rationale for doing an unsigned comparion instead of a
    signed comparison in case of mixed arguments?
     
    Olaf van der Spek, May 6, 2006
    #5
  6. * Olaf van der Spek:
    Integer promotion comes into play. That's primarily designed for
    assignment, promoting to types with ever widening range of values that
    can be represented. 'unsigned' can /represent/ all 'int' values (you
    can convert from 'int' to 'unsigned' with well-defined unique result)
    but not vice versa.
     
    Alf P. Steinbach, May 6, 2006
    #6
  7. Timothee Groleau

    john Guest

    How is the int value -1 represented in unsigned int?
     
    john, May 6, 2006
    #7
  8. * john:
    Modulo 2^n, where n is the number of bits in the value representation of
    unsigned int.

    For the exact value (if that's not obvious), see the earlier messages in
    this thread, where it was used as an example.
     
    Alf P. Steinbach, May 6, 2006
    #8
  9. Timothee Groleau

    Jim Langston Guest

    The easiest way for you to figure that out is to try it.

    std::cout << (unsigned int)(-1)

    or to make sure
    int MyInt = -1;
    std::cout << (unsigned int) MyInt;
     
    Jim Langston, May 6, 2006
    #9
  10. Timothee Groleau

    john Guest

    Ok ,thanks, i see.

    But why is the opposite, unsigned to int, undefined?
    Shouldn't it just be the other way around?
     
    john, May 6, 2006
    #10
  11. Why is unsigned int -> int not well-defined?
    And I disagree, unsigned(-1) may have the same bit pattern but doesn't
    represent the value -1 IMO.
     
    Olaf van der Spek, May 6, 2006
    #11
  12. Does the C++ standard require two's complement representation?
     
    Olaf van der Spek, May 6, 2006
    #12
  13. Timothee Groleau

    Pete Becker Guest

    It's a more or less arbitrary choice. For example, with 16-bit integers,
    a signed int can hold values in the range -32767 to 32767; an unsigned
    int can hold values in the range 0 to 65535. Going either way, there
    will be values that can't be "correctly" represented. C chose to define
    unsigned arithmetic more rigorously than signed, and C++ followed along.
     
    Pete Becker, May 6, 2006
    #13
  14. * Olaf van der Spek:
    No, but it requires the modulo 2^n "conversion" from signed to unsigned.
     
    Alf P. Steinbach, May 6, 2006
    #14
  15. Timothee Groleau

    persenaama Guest

    And I disagree, unsigned(-1) may have the same bit pattern but doesn't
    unsigned int a = 4;
    unsigned int b = 8;
    unsigned int c = a - b; // any comment on this?
    unsigned int d = c + 10; // how about this?

    Looks to me that "negative" values are indeed not presented as such,
    but atleast the arithmetic is guaranteed to work. Works for me. :)
     
    persenaama, May 6, 2006
    #15
  16. Timothee Groleau

    persenaama Guest

    FYI, the point is that how you ever going to know if unsigned int is
    supposed to be 'negative' or not: you don't - the values are always,
    absolutely, positively, positive.

    In this case, ofcourse, you can take a look at a and b, but that is
    besides the point already.

    There comes a time, when you absolutely, positively, have to have
    negate of a specific value. Want to know the lowest set bit in unsigned
    integer?

    pseudo: v & -v

    If v is unsigned int, doing unary minus is kind of out of the question.
    So you put your ninja outfit on and write instead: v & (0 - v) and the
    compiler is none the wiser but compiles your crime against c++
    programming. You leave the crime scene with the lowest set bit richer.
     
    persenaama, May 6, 2006
    #16
  17. Timothee Groleau

    Pete Becker Guest

    No, it's well defined. The value is 2^n-v, where n is the number of bits
    in an unsigned int.
    All this graphic language obscures the fact that both of these
    expressions have precisely defined meanings and they do exactly the same
    thing, which is exactly what you want in this example. It may be that
    your compiler is giving you warnings, but most compiler warnings these
    days are about style and not about substance.
     
    Pete Becker, May 6, 2006
    #17
  18. I didn't say c and d are undefined in C++.
    But I do say that c does not contain the value -4.
     
    Olaf van der Spek, May 7, 2006
    #18
  19. Timothee Groleau

    persenaama Guest

    I got that, merely experienced a "so what!?" -moment and shared the
    love. :)
     
    persenaama, May 7, 2006
    #19
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.