| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Timothee Groleau" |
| Date: |
06 May 2006 05:04:23 AM |
| Object: |
comparison signed vs unsigned |
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.
.
|
|
| User: "Rolf Magnus" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 05:29:41 AM |
|
|
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';
}
.
|
|
|
| User: "Olaf van der Spek" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 06:27:13 AM |
|
|
On Sat, 06 May 2006 12:29:41 +0200, Rolf Magnus <ramagnus@t-online.de>
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?
.
|
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 06:44:03 AM |
|
|
* Olaf van der Spek:
On Sat, 06 May 2006 12:29:41 +0200, Rolf Magnus <ramagnus@t-online.de>
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?
.
|
|
|
| User: "john" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 06:54:27 AM |
|
|
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?
.
|
|
|
| User: "Jim Langston" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 07:29:46 AM |
|
|
"john" <no@email.com> wrote in message
news:T707g.55886$d5.210408@newsb.telia.net...
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;
.
|
|
|
| User: "john" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 07:37:58 AM |
|
|
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?
.
|
|
|
| User: "Pete Becker" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 10:28:31 AM |
|
|
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.
.
|
|
|
|
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 07:13:40 AM |
|
|
* 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?
.
|
|
|
| User: "Olaf van der Spek" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 09:02:52 AM |
|
|
On Sat, 06 May 2006 14:13:40 +0200, "Alf P. Steinbach"
<alfps@start.no> 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?
.
|
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 11:15:32 AM |
|
|
* Olaf van der Spek:
On Sat, 06 May 2006 14:13:40 +0200, "Alf P. Steinbach"
<alfps@start.no> 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?
.
|
|
|
|
|
|
|
| User: "Olaf van der Spek" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 09:02:10 AM |
|
|
On Sat, 06 May 2006 13:44:03 +0200, "Alf P. Steinbach"
<alfps@start.no> wrote:
* Olaf van der Spek:
On Sat, 06 May 2006 12:29:41 +0200, Rolf Magnus <ramagnus@t-online.de>
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.
.
|
|
|
| User: "persenaama" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 12:07:38 PM |
|
|
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. :)
.
|
|
|
| User: "persenaama" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 03:24:24 PM |
|
|
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.
.
|
|
|
| User: "Pete Becker" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 03:51:43 PM |
|
|
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.
.
|
|
|
|
|
| User: "Olaf van der Spek" |
|
| Title: Re: comparison signed vs unsigned |
07 May 2006 06:11:32 AM |
|
|
On 6 May 2006 10:07:38 -0700, "persenaama" <jukka@liimatta.org> 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.
.
|
|
|
| User: "persenaama" |
|
| Title: Re: comparison signed vs unsigned |
07 May 2006 10:40:45 AM |
|
|
I got that, merely experienced a "so what!?" -moment and shared the
love. :)
.
|
|
|
|
|
|
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 05:32:39 AM |
|
|
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';
}
.
|
|
|
| User: "Timothee Groleau" |
|
| Title: Re: comparison signed vs unsigned |
06 May 2006 05:59:10 AM |
|
|
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!
.
|
|
|
|
|
|

|
Related Articles |
|
|