473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

2 unsigned shorts make a signed int ???

I have something funny happening when I compile (gcc)

I have two variables:

Expand|Select|Wrap|Line Numbers
  1. unsigned short x;
  2. unsigned short y;
  3.  

and when I used them in a for loop condition:

Expand|Select|Wrap|Line Numbers
  1. unsigned int i;
  2. for( i = 0 ; i < x * y ; i++ )
  3. { ; }
  4.  

I get a compiler warning:
" warning: comparison between signed and unsigned "

so since my "i" is unsigned, the x*y must have been converted to signed.......


I know it's being converted to a 32 bit int because sizeof = 4

Can someone tell me what's going on?
thanks!!

p.s. i know that it's bad practice to compute in the condition
Jun 3 '11 #1

✓ answered by donbock

The usual unary conversions are applied in turn to each operand in an expression. They serve to make the job of the compiler's expression evaluator easier by limiting the number of types that must be supported. One of these rules is:
An unsigned type of rank less than int, all of whose values can be represented by type int is converted to int.
The usual binary conversions are applied when an expression is evaluated. The usual binary conversions occur after the usual unary conversions and serve to convert all operands to a single common type, which is typically also the type of the result. One of the these rules is:
If either operand has any signed type and the other operand has any signed type, then both are converted to the signed type with the greater rank.
In your case, the usual unary conversions converts both x and y into ints. The usual binary conversions are satisfied to leave them as ints and it determines that the result will also be an int. Thus, your logical expression compares unsigned int i to the signed int result of the multiplication. The usual conversions have rules to handle this operation, but it is not uncommon to get a compiler warning to alert you that types whose domains do not overlap are being used.

By the way, this is why you don't always get major improvements in execution time by using the smallest possible types. Your variables might be stored in chars or shorts, but your arithmetic still takes place with ints.

3 2506
donbock
2,426 Expert 2GB
The usual unary conversions are applied in turn to each operand in an expression. They serve to make the job of the compiler's expression evaluator easier by limiting the number of types that must be supported. One of these rules is:
An unsigned type of rank less than int, all of whose values can be represented by type int is converted to int.
The usual binary conversions are applied when an expression is evaluated. The usual binary conversions occur after the usual unary conversions and serve to convert all operands to a single common type, which is typically also the type of the result. One of the these rules is:
If either operand has any signed type and the other operand has any signed type, then both are converted to the signed type with the greater rank.
In your case, the usual unary conversions converts both x and y into ints. The usual binary conversions are satisfied to leave them as ints and it determines that the result will also be an int. Thus, your logical expression compares unsigned int i to the signed int result of the multiplication. The usual conversions have rules to handle this operation, but it is not uncommon to get a compiler warning to alert you that types whose domains do not overlap are being used.

By the way, this is why you don't always get major improvements in execution time by using the smallest possible types. Your variables might be stored in chars or shorts, but your arithmetic still takes place with ints.
Jun 3 '11 #2
where can I find these rules? I searched the GNU C Library Reference Manual and GNU Compiler Collection manual and I can't find the rules you posted above in either one. thanks so much for helping!
Jun 8 '11 #3
donbock
2,426 Expert 2GB
I found them in chapter 6.3 (The Usual Conversions) in "C: A Reference Manual", 5th edition, Harbison and Steele, 2002, ISBN 0-13-089592X. You can also take a look at section 6.3 (Conversions) of the C99 specification.
Jun 8 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sarmin kho | last post by:
Hi Pythoners, When it is an integer number, what is the range of the integer number and long integer number?? do we have typecasting of 8bits or 16bits signed and unsigned number in python? ...
6
by: Sona | last post by:
Hi, What is the difference between a signed 0x00 (NULL, or 0) and an unsigned 0x00? Can there be one? If I do the following: char var; var = 0x00; what should var hold? and if it was an...
3
by: codefixer | last post by:
Hi, I was wondering what will be the output for this piece of code. I am in confusion regarding the promotions of bitfield. If your reply is to compile, execute and check out myself, Thank you...
1
by: Steffen Fiksdal | last post by:
I have a function that base64 decodes some data. The incoming data is received as "const char*" (BASE64 characters are always safe ASCII characters, meaning they will always fit in a signed char...
4
by: Sriniv | last post by:
Hello all, Do we have unsigned and signed char types? If so, i'd like to know the differences between the two. And one more question. Is there any provision in C for using the Unicode character...
19
by: junky_fellow | last post by:
How the unsigned to signed integet conversion is done ? For eg: unsigned int ui = 100; int si = ui; What will be the value if "si" is printed ? How this conversion is done ? Thanx for any...
14
by: me2 | last post by:
I am writing a little base conversion utility called base.c. This is what base does. $ base -127 Signed decimal: -127 Unsigned decimal: 4294967169 Hexidecimal: ...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
9
by: Ioannis Vranos | last post by:
Under C++03: Is it guaranteed that char, unsigned char, signed char have no padding bits?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.