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

  2. Timothee Groleau

    Rolf Magnus Guest

    Timothee Groleau wrote:

    > 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?


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

  3. Timothee Groleau

    Rolf Magnus Guest

    Rolf Magnus wrote:

    > Timothee Groleau wrote:
    >
    >> 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?

    >
    > #include <iostream>
    >
    > int main()
    > {
    > int a = -100;
    > unsigned int b = 100;
    > std::cout << a << " is "
    > << (a < b ? "" : "not") << " less than " << b << '\n';
    > }


    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. Rolf Magnus wrote:

    > #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';
    > }


    Thanks Rolf!
    Timothee Groleau, May 6, 2006
    #4
  5. On Sat, 06 May 2006 12:29:41 +0200, Rolf Magnus <>
    wrote:

    >Timothee Groleau wrote:
    >
    >> 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?

    >
    >#include <iostream>
    >
    >int main()
    >{
    > int a = -100;
    > unsigned int b = 100;
    > std::cout << a << " is "
    > << (a < b ? "" : "not") << " less than " << b << '\n';
    >}


    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:
    > On Sat, 06 May 2006 12:29:41 +0200, Rolf Magnus <>
    > wrote:
    >
    >> Timothee Groleau wrote:
    >>
    >>> 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?

    >> #include <iostream>
    >>
    >> int main()
    >> {
    >> int a = -100;
    >> unsigned int b = 100;
    >> std::cout << a << " is "
    >> << (a < b ? "" : "not") << " less than " << b << '\n';
    >> }

    >
    > What's the rationale for doing an unsigned comparion instead of a
    > signed comparison in case of mixed arguments?


    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.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 6, 2006
    #6
  7. Timothee Groleau

    john Guest

    Alf P. Steinbach wrote:
    >
    > 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.
    >


    How is the int value -1 represented in unsigned int?
    john, May 6, 2006
    #7
  8. * john:
    > Alf P. Steinbach wrote:
    >>
    >> 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.
    >>

    >
    > How is the int value -1 represented in unsigned int?


    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.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 6, 2006
    #8
  9. Timothee Groleau

    Jim Langston Guest

    "john" <> wrote in message
    news:T707g.55886$...
    > Alf P. Steinbach wrote:
    >>
    >> 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.
    >>

    >
    > How is the int value -1 represented in unsigned int?


    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

    Jim Langston wrote:

    > 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;
    >
    >


    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. On Sat, 06 May 2006 13:44:03 +0200, "Alf P. Steinbach"
    <> wrote:

    >* Olaf van der Spek:
    >> On Sat, 06 May 2006 12:29:41 +0200, Rolf Magnus <>
    >> wrote:
    >>
    >>> Timothee Groleau wrote:
    >>>
    >>>> 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?
    >>> #include <iostream>
    >>>
    >>> int main()
    >>> {
    >>> int a = -100;
    >>> unsigned int b = 100;
    >>> std::cout << a << " is "
    >>> << (a < b ? "" : "not") << " less than " << b << '\n';
    >>> }

    >>
    >> What's the rationale for doing an unsigned comparion instead of a
    >> signed comparison in case of mixed arguments?

    >
    >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.


    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. On Sat, 06 May 2006 14:13:40 +0200, "Alf P. Steinbach"
    <> wrote:

    >* john:
    >> Alf P. Steinbach wrote:
    >>>
    >>> 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.
    >>>

    >>
    >> How is the int value -1 represented in unsigned int?

    >
    >Modulo 2^n, where n is the number of bits in the value representation of
    >unsigned int.


    Does the C++ standard require two's complement representation?
    Olaf van der Spek, May 6, 2006
    #12
  13. Timothee Groleau

    Pete Becker Guest

    john wrote:
    >
    > But why is the opposite, unsigned to int, undefined?
    > Shouldn't it just be the other way around?


    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
    Roundhouse Consulting, Ltd.
    Pete Becker, May 6, 2006
    #13
  14. * Olaf van der Spek:
    > On Sat, 06 May 2006 14:13:40 +0200, "Alf P. Steinbach"
    > <> wrote:
    >
    >> * john:
    >>> Alf P. Steinbach wrote:
    >>>> 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.
    >>>>
    >>> How is the int value -1 represented in unsigned int?

    >> Modulo 2^n, where n is the number of bits in the value representation of
    >> unsigned int.

    >
    > Does the C++ standard require two's complement representation?


    No, but it requires the modulo 2^n "conversion" from signed to unsigned.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    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
    > represent the value -1 IMO.


    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

    persenaama wrote:
    >
    > 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.


    No, it's well defined. The value is 2^n-v, where n is the number of bits
    in an unsigned int.

    > 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.
    >


    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
    Roundhouse Consulting, Ltd.
    Pete Becker, May 6, 2006
    #17
  18. On 6 May 2006 10:07:38 -0700, "persenaama" <> wrote:

    >> And I disagree, unsigned(-1) may have the same bit pattern but doesn't
    >> represent the value -1 IMO.

    >
    >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. :)


    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. 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. g
    Replies:
    0
    Views:
    3,907
  2. John Buckley

    comparison between signed and unsigned

    John Buckley, Oct 23, 2003, in forum: C Programming
    Replies:
    2
    Views:
    477
    Dan Pop
    Oct 24, 2003
  3. Alex
    Replies:
    3
    Views:
    643
    Michael Mair
    Apr 26, 2006
  4. Gary Wessle
    Replies:
    4
    Views:
    629
    Ian Collins
    Jul 29, 2006
  5. Replies:
    6
    Views:
    383
    Army1987
    Sep 21, 2007
Loading...

Share This Page