Bitwise Not Operator

Discussion in 'C Programming' started by aleksa, Jun 26, 2012.

  1. aleksa

    aleksa Guest

    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))
     
    aleksa, Jun 26, 2012
    #1
    1. Advertising

  2. aleksa

    Ike Naar Guest

    On 2012-06-26, aleksa <> 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?
     
    Ike Naar, Jun 26, 2012
    #2
    1. Advertising

  3. aleksa

    Les Cargill Guest

    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
     
    Les Cargill, Jun 26, 2012
    #3
  4. aleksa

    aleksa Guest

    On Tuesday, June 26, 2012 7:06:01 PM UTC+2, Ike Naar wrote:
    > On 2012-06-26, aleksa <> 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?
     
    aleksa, Jun 26, 2012
    #4
  5. aleksa

    Jens Gustedt Guest

    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 <> 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
     
    Jens Gustedt, Jun 26, 2012
    #5
  6. aleksa

    James Kuyper Guest

    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 <> 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
     
    James Kuyper, Jun 26, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. littlehobo

    Bitwise operator question...

    littlehobo, Sep 30, 2003, in forum: C++
    Replies:
    10
    Views:
    6,085
    Ron Natalie
    Oct 1, 2003
  2. Mantorok Redgormor

    bitwise not operator

    Mantorok Redgormor, Oct 1, 2003, in forum: C Programming
    Replies:
    1
    Views:
    550
    Dan Pop
    Oct 1, 2003
  3. Manish_Ganvir
    Replies:
    13
    Views:
    1,632
    Keith Thompson
    Feb 14, 2005
  4. Ioannis Vranos
    Replies:
    8
    Views:
    357
    James Kanze
    Nov 14, 2008
  5. Matt Kruse

    Bitwise NOT operator ( ~ )

    Matt Kruse, May 12, 2009, in forum: Javascript
    Replies:
    3
    Views:
    138
    Dr J R Stockton
    May 16, 2009
Loading...

Share This Page