473,326 Members | 2,168 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.

Comparison between signed and unsigned

Hello - I have this simple code:

Expand|Select|Wrap|Line Numbers
  1. void funct_name(void *a, UINT32 *count)
  2. {
  3.   UINT32 *p = (UINT32 *) a;
  4.   UINT32 *q = p;
  5.  
  6.   for( q=p; *q != ~0; q++)
  7.     do something...;
  8. }
  9.  
i am pretty new to C and i think i am not following the pointers correctly. The UINT32 is of course a 32 bit unsigned int. Can someone explain to me why I get a "warning: comparison between signed and unsigned" message?

please advise
thanks!
Jul 16 '08 #1
7 3463
JosAH
11,448 Expert 8TB
The error diagnostic also mentioned the line number; the only comparison is on
line 6: *q != ~0 and there you can see exactly what's happening: you are comparing
an unsigned int (*q) with a signed int (~0) and that's what the compiler is warning
you about. Always read what the compiler is whining about; it has a reason.

kind regards,

Jos
Jul 17 '08 #2
Would you mind explaining to me how ~0 is a signed int? How do you know its signed? Isn't it just zero, with all the bits inverted? I think it could be unsigned 0 with all the bits inverted...?

Thanks!
Jul 17 '08 #3
JosAH
11,448 Expert 8TB
Would you mind explaining to me how ~0 is a signed int? How do you know its signed? Isn't it just zero, with all the bits inverted? I think it could be unsigned 0 with all the bits inverted...?

Thanks!
By definition any int literal is signed no matter its value. Of course an unsigned
int could represent this particular value as well but the compile doesn't know that.

kind regards,

Jos
Jul 17 '08 #4
I understand :)

how does one make an int literal unsigned? I can't cast a literal, can I? I would like to eliminate this error.

Thanks!
Jul 17 '08 #5
boxfish
469 Expert 256MB
I think you can cast a literal. It works for me. Try it.
Jul 17 '08 #6
its does get rid of the error thank you for all the information!
Jul 17 '08 #7
weaknessforcats
9,208 Expert Mod 8TB
Just assign your literal value to an unsigned int and use that in the comparision.

The compiler does not look at the value of the literal in a compare. The literal is assumed signed.

However, you can assign a literal to an unsigned int since you are assigning an integer to and interger. The rules permit the assignment of any interger type to any other integer type. In this case the compiler assumes you know what you are doing.

Expand|Select|Wrap|Line Numbers
  1. unsigned int a = 0;
  2. unsigned int b = -1;
  3.  
In the above example, there is not a whimper from the compiler. Here the sign bit from -1 becomes a data bit in b. Presumably, there could be a reason for this so the assignment is allowed.
Jul 17 '08 #8

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

Similar topics

0
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
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
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) {
18
by: Timothee Groleau | last post by:
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.
2
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
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
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
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
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
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.