Bitwise Not Operator

A

aleksa

unsigned char X, Y;
if (X != (unsigned char) ~Y)

why do I get a warning here?
comparison of promoted ~unsigned with unsigned

As I understand, ~ promotes it to int,
but haven't I casted it back to a byte?


And, why does it work here?

unsigned char notb (unsigned char val) {
return ~val;
}

if (X != notb(Y))
 
I

Ike Naar

unsigned char X, Y;
if (X != (unsigned char) ~Y)

why do I get a warning here?
comparison of promoted ~unsigned with unsigned

This is unexpected.
Looking a the format of the warning, it seems you're using gcc.
But gcc 4.1.2 only gives this warning when the cast to unsigned char
is missing, as in (X != ~Y). If the cast is in place, the compiler
remains silent. Which version of gcc do you use?
 
L

Les Cargill

aleksa said:
unsigned char X, Y;
if (X != (unsigned char) ~Y)

why do I get a warning here?
comparison of promoted ~unsigned with unsigned

As I understand, ~ promotes it to int,
but haven't I casted it back to a byte?


And, why does it work here?

unsigned char notb (unsigned char val) {
return ~val;
}

if (X != notb(Y))


Dunno why you get a warning:

C:\c\usenet>gcc -v

Reading specs from c:/Mingw/bin/../lib/gcc/mingw3....

gcc version 3.4.5 (mingw-vista special r3)

C:\c\usenet>


C:\c\usenet>gcc -Wall --pedantic -o not.exe not.c

C:\c\usenet>
C:\c\usenet>cat not.c

#include <stdio.h>

int main(void)
{

unsigned char X=9, Y=42;
if (X != (unsigned char) ~Y)
printf("uh uhh\n");


return 0;
}
 
A

aleksa

This is unexpected.
Looking a the format of the warning, it seems you're using gcc.
But gcc 4.1.2 only gives this warning when the cast to unsigned char
is missing, as in (X != ~Y). If the cast is in place, the compiler
remains silent. Which version of gcc do you use?

4.6.2.

Actually, the original warning message is ended with [-Wsign-compare]
don't know why I haven't included that part...
(that option is included with -Wextra).

-Wsign-compare:
"Warn when a comparison between signed and unsigned values..."
but, aren't both values here unsigned?
 
J

Jens Gustedt

Am 26.06.2012 20:40, schrieb aleksa:
but, aren't both values here unsigned?

no, Y is promoted to int before the ~ is applied, then ~Y is int, too

all types that have an integer rank lower than int are promoted to int
(or on rare architectures to unsigned) before any operator is applied

Jens
 
J

James Kuyper

This is unexpected.
Looking a the format of the warning, it seems you're using gcc.
But gcc 4.1.2 only gives this warning when the cast to unsigned char
is missing, as in (X != ~Y). If the cast is in place, the compiler
remains silent. Which version of gcc do you use?

4.6.2.

Actually, the original warning message is ended with [-Wsign-compare]
don't know why I haven't included that part...
(that option is included with -Wextra).

-Wsign-compare:
"Warn when a comparison between signed and unsigned values..."
but, aren't both values here unsigned?

Both values start out as unsigned, but if UCHAR_MAX <= INT_MAX, both X
and (unsigned char)~Y get promoted to int before the comparison is
performed. However, regardless of whether it's looking at the original
types or the promoted types, and regardless of whether UCHAR_MAX >
INT_MAX, the signedness is the same on both sides of the expression, so
I don't understand why the compiler would generate that particular
warning. It's the ~Y expression that needs warning about. It's generally
a bad idea to perform bitwise operations on signed types, and due to the
integer promotions, that's precisely what ~Y does (unless UCHAR_MAX >
INT_MAX). What you probably want is something like:

X != UCHAR_MAX & ~(unsigned)Y
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top