473,799 Members | 3,185 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 9457
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_li mits<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_li mits<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******** ***********@new sb.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

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

Similar topics

0
2923
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 C++ version 3.x), but perhaps it's more general than that. The following program (to be compiled with g++ 3.x, 2.9x is not enough) #include <iostream> #include <string>
1
6156
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
11036
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
5124
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 promoted to "signed int": signed char
4
16242
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 loooong vector and loop in an incremental way to select the column of choice, here is the code which works but gives a warning ****************************************************************
8
3894
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 into a memory buffer. It is defined char sram; I need to check for a specific value within this buffer.
14
24283
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 MultiByte String env (MBCS) and wchar_t if UNICODE #if defined(WIN32) || defined(UNDER_CE)
6
11163
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 unsigned conversion. Either b is upgraded to unsigned and the resulting value during comparison is 5 or a is downgraded to signed and the resulting value is lower than -1.
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 what is the best way to prevent it. I can just change i to a type size_t or maybe put a cast in the conditional, but I don't know which way that is 'best'? Any ideas? I provided the code below for some context. And while we're at it, could you...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.