comparison signed vs unsigned

  • Thread starter Timothee Groleau
  • Start date
T

Timothee Groleau

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

Rolf Magnus

Timothee said:
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';
}
 
R

Rolf Magnus

Rolf said:
#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';
}
 
T

Timothee Groleau

Rolf said:
#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!
 
O

Olaf van der Spek

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

Alf P. Steinbach

* Olaf van der Spek:
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.
 
J

john

Alf said:
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?
 
A

Alf P. Steinbach

* john:
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.
 
J

Jim Langston

john said:
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;
 
J

john

Jim said:
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?
 
O

Olaf van der Spek

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

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

Olaf van der Spek

* john:

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

Pete Becker

john said:
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.
 
A

Alf P. Steinbach

* Olaf van der Spek:
Does the C++ standard require two's complement representation?

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

persenaama

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

persenaama

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

Pete Becker

persenaama said:
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.
 
O

Olaf van der Spek

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top