473,503 Members | 3,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
May 6 '06 #1
18 9413
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';
}
May 6 '06 #2
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';
}

May 6 '06 #3
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!

May 6 '06 #4
On Sat, 06 May 2006 12:29:41 +0200, Rolf Magnus <ra******@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?
May 6 '06 #5
* Olaf van der Spek:
On Sat, 06 May 2006 12:29:41 +0200, Rolf Magnus <ra******@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?
May 6 '06 #6
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?
May 6 '06 #7
* 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?
May 6 '06 #8
"john" <no@email.com> wrote in message
news:T7*******************@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;

May 6 '06 #9
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?
May 6 '06 #10
On Sat, 06 May 2006 13:44:03 +0200, "Alf P. Steinbach"
<al***@start.no> wrote:
* Olaf van der Spek:
On Sat, 06 May 2006 12:29:41 +0200, Rolf Magnus <ra******@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.
May 6 '06 #11
On Sat, 06 May 2006 14:13:40 +0200, "Alf P. Steinbach"
<al***@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?
May 6 '06 #12
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.
May 6 '06 #13
* Olaf van der Spek:
On Sat, 06 May 2006 14:13:40 +0200, "Alf P. Steinbach"
<al***@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?
May 6 '06 #14
> 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. :)

May 6 '06 #15
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.

May 6 '06 #16
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.
May 6 '06 #17
On 6 May 2006 10:07:38 -0700, "persenaama" <ju***@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.
May 7 '06 #18
I got that, merely experienced a "so what!?" -moment and shared the
love. :)

May 7 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2899
by: g | last post by:
Hi all. Maybe this question has been asked many times before, but I was not able to find any pointer. I apologize in advance, for it refers to a particular standard library implementation (GNU...
1
6040
by: Paul Edwards | last post by:
I have the following C program: #include <stdio.h> int main(void) { unsigned short x = 32768, y=16384; if (x > y) {
3
11020
by: Alex | last post by:
I have a problem about the comparison between signed and unsigned integer. See the following code: int foo() { int i; unsigned int j; if (i < j) {
2
5108
by: Frederick Gotham | last post by:
I just want to clarify my understanding of arithmetic and comparison between two different integer types. Phase (1): Integer Promotion ---------- All of the following types always get...
4
16203
by: Gary Wessle | last post by:
Hi I am writing a code to open a space delimited data file, return the number of rows and columns, as well as return the nth column where n is user define number. I put all the cells in a...
8
3877
by: John Ratliff | last post by:
"comparison is always false due to limited range of data type" I get this warning with g++ when I compile some code and I'm not quite sure I understand it. I have a small file that I've read...
14
24242
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
6
11144
by: compcreator | last post by:
I have tried the following program. The problem is it is printing False I checked values for a and b but there is something wrong with the comparison. I thing it might be because of signed and...
5
695
by: evanevankan2 | last post by:
I have a question about the warning 'comparison between signed and unsigned' I get from my code. It comes from the conditional in the outer for loop. I understand the warning, but I'm not sure...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.