Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22803 > unrolled thread

Bitwise Not Operator

Started byaleksa <aleksazr@gmail.com>
First post2012-06-26 08:35 -0700
Last post2012-06-26 12:31 -0500
Articles 6 — 5 participants

Back to article view | Back to comp.lang.c


Contents

  Bitwise Not Operator aleksa <aleksazr@gmail.com> - 2012-06-26 08:35 -0700
    Re: Bitwise Not Operator Ike Naar <ike@iceland.freeshell.org> - 2012-06-26 17:06 +0000
      Re: Bitwise Not Operator aleksa <aleksazr@gmail.com> - 2012-06-26 11:40 -0700
        Re: Bitwise Not Operator Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-26 20:57 +0200
        Re: Bitwise Not Operator James Kuyper <jameskuyper@verizon.net> - 2012-06-26 15:17 -0400
    Re: Bitwise Not Operator Les Cargill <lcargill99@comcast.com> - 2012-06-26 12:31 -0500

#22803 — Bitwise Not Operator

Fromaleksa <aleksazr@gmail.com>
Date2012-06-26 08:35 -0700
SubjectBitwise Not Operator
Message-ID<5baf9e7b-f46f-4aad-9e44-aa4c12cb0041@googlegroups.com>
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))

[toc] | [next] | [standalone]


#22811

FromIke Naar <ike@iceland.freeshell.org>
Date2012-06-26 17:06 +0000
Message-ID<slrn3vfsjujqvp.jur.ike@iceland.freeshell.org>
In reply to#22803
On 2012-06-26, aleksa <aleksazr@gmail.com> wrote:
> 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?

[toc] | [prev] | [next] | [standalone]


#22815

Fromaleksa <aleksazr@gmail.com>
Date2012-06-26 11:40 -0700
Message-ID<12aac795-58c8-4810-b017-4034c655bf50@googlegroups.com>
In reply to#22811
On Tuesday, June 26, 2012 7:06:01 PM UTC+2, Ike Naar wrote:
> On 2012-06-26, aleksa <aleksazr@gmail.com> wrote:
> > 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?

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?

[toc] | [prev] | [next] | [standalone]


#22816

FromJens Gustedt <jens.gustedt@loria.fr>
Date2012-06-26 20:57 +0200
Message-ID<4FEA0621.8060707@loria.fr>
In reply to#22815
Am 26.06.2012 20:40, schrieb aleksa:
> On Tuesday, June 26, 2012 7:06:01 PM UTC+2, Ike Naar wrote:
>> On 2012-06-26, aleksa <aleksazr@gmail.com> wrote:
>>> unsigned char X, Y;
>>> if (X != (unsigned char) ~Y)
>>>
>>> why do I get a warning here?
>>> comparison of promoted ~unsigned with unsigned

> 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




[toc] | [prev] | [next] | [standalone]


#22820

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-26 15:17 -0400
Message-ID<4FEA0AE7.2020308@verizon.net>
In reply to#22815
On 06/26/2012 02:40 PM, aleksa wrote:
> On Tuesday, June 26, 2012 7:06:01 PM UTC+2, Ike Naar wrote:
>> On 2012-06-26, aleksa <aleksazr@gmail.com> wrote:
>>> 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?
> 
> 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

[toc] | [prev] | [next] | [standalone]


#22812

FromLes Cargill <lcargill99@comcast.com>
Date2012-06-26 12:31 -0500
Message-ID<jscrg6$l7e$1@dont-email.me>
In reply to#22803
aleksa wrote:
> 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;
}


--
Les Cargill


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web