Bitwise complement

Discussion in 'C Programming' started by Martin Kojtal, Jul 18, 2012.

  1. Hello,

    the example application is running on 8bit MCU, register is 8 bit wide. There's line where it has to clear the flag with following command:

    StructPointer->REGISTER &= ~(FIRST_BIT_MASK);

    It issues warning "possible loss of data" which I understand because ~ operator have operands integer type (6.5.4 Expressions). Why the warning disappears when the bit mask is casted to UINT8:

    StructPointer->REGISTER &= ~((UINT8)FIRST_BIT_MASK);

    I am inclined to cast the result of bitwise complement: (UINT8)(~FIRST_BIT_MASK).
    Are both forms correct to suppress the warning?

    MartinK
    Martin Kojtal, Jul 18, 2012
    #1
    1. Advertising

  2. Martin Kojtal

    Eric Sosman Guest

    On 7/18/2012 4:56 AM, Martin Kojtal wrote:
    > Hello,
    >
    > the example application is running on 8bit MCU, register is 8 bit wide. There's line where it has to clear the flag with following command:
    >
    > StructPointer->REGISTER &= ~(FIRST_BIT_MASK);
    >
    > It issues warning "possible loss of data" which I understand because ~ operator have operands integer type (6.5.4 Expressions). Why the warning disappears when the bit mask is casted to UINT8:
    >
    > StructPointer->REGISTER &= ~((UINT8)FIRST_BIT_MASK);
    >
    > I am inclined to cast the result of bitwise complement: (UINT8)(~FIRST_BIT_MASK).
    > Are both forms correct to suppress the warning?


    The compiler can issue warnings for any reason, even for C code
    that is perfectly valid. Sometimes you'll be able to silence a
    warning by changing one valid fragment to another, sometimes not.
    (Sometimes, a rewrite that placates one compiler will upset another!)
    In your case it looks like the (UINT8) cast silences your compiler's
    warning -- but "Why?" is a question about the compiler, not about C.

    We can't tell whether your two versions are equivalent, because
    we don't know the data types of FIRST_BIT_MASK and REGISTER (we
    don't actually *know* what UINT8 is, either, though we might guess).
    Under reasonable assumptions about the data types and about the way
    your machine probably represents negative integers, the two versions
    do the same thing -- so the compiler's nervousness about one spelling
    and acceptance of the other is not a C thing, but a compiler thing.
    Perhaps the compiler's documentation says something about the matter.

    --
    Eric Sosman
    d
    Eric Sosman, Jul 18, 2012
    #2
    1. Advertising

  3. Martin Kojtal

    ImpalerCore Guest

    On Jul 18, 4:56 am, Martin Kojtal <> wrote:
    > Hello,
    >
    > the example application is running on 8bit MCU, register is 8 bit wide. There's line where it has to clear the flag with following command:
    >
    > StructPointer->REGISTER &= ~(FIRST_BIT_MASK);
    >
    > It issues warning "possible loss of data" which I understand because ~ operator have operands integer type (6.5.4 Expressions). Why the warning disappears when the bit mask is casted to UINT8:
    >
    > StructPointer->REGISTER &= ~((UINT8)FIRST_BIT_MASK);
    >
    > I am inclined to cast the result of bitwise complement: (UINT8)(~FIRST_BIT_MASK).
    > Are both forms correct to suppress the warning?


    This warning may appear because the integer width of FIRST_BIT_MASK is
    larger than REGISTER. Often 'int' is a 16-bit integer in many 8-bit
    PIC compilers.

    If you had some form of <stdint.h>, you could possibly get around this
    by defining FIRST_BIT_MASK using a UINT8_C(0x..) constant, or you
    could include the (UINT8) cast in the definition of FIRST_BIT_MASK,
    i.e. #define FIRST_BIT_MASK ((UINT8)0xA0).

    Best regards,
    John D.
    ImpalerCore, Jul 18, 2012
    #3
  4. Martin Kojtal

    Les Cargill Guest

    Martin Kojtal wrote:
    > Hello,
    >
    > the example application is running on 8bit MCU, register is 8 bit wide. There's line where it has to clear the flag with following command:
    >
    > StructPointer->REGISTER &= ~(FIRST_BIT_MASK);
    >
    > It issues warning "possible loss of data" which I understand because ~ operator have operands integer type (6.5.4 Expressions). Why the warning disappears when the bit mask is casted to UINT8:
    >
    > StructPointer->REGISTER &= ~((UINT8)FIRST_BIT_MASK);
    >


    Because that probably neutralizes the risk of sign extension causing
    problems.

    > I am inclined to cast the result of bitwise complement: (UINT8)(~FIRST_BIT_MASK).
    > Are both forms correct to suppress the warning?
    >
    > MartinK
    >



    Seriously, just turn on -Wall and --strict-whatever and experiment. If
    you're skeptical, dump the assembly and look at it.

    --
    Les Cargill
    Les Cargill, Jul 18, 2012
    #4
  5. Martin Kojtal

    Phil Carmody Guest

    Kenneth Brody <> writes:
    > On 7/18/2012 4:56 AM, Martin Kojtal wrote:
    > > StructPointer->REGISTER &= ~(FIRST_BIT_MASK);

    ....
    > However, I think all of the replies have forgotten integer promotions.
    > Quoting section 6.3.1.1p2 footnote 48:
    >
    > > The integer promotions are applied only: as part of the usual arithmetic
    > > conversions, to certain argument expressions, to the operands of the
    > > unary +, -, and ~ operators, and to both operands of the shift operators,
    > > as specified by their respective subclauses.

    >
    > So, as I read it, the unary "~" promotes the UINT8 to an int.


    More pedantically - before the ~ operates, its operand is promoted
    to an int, rather than the operation simply yielding an int from
    a UINT8.

    (Corner case wacko definitions of UINT8 ignored.)

    > This seems to be confirmed on my system as:
    >
    > Given:
    >
    > char c;
    >
    > Then:
    >
    > sizeof(c) == 1
    > sizeof(~c) == 4


    Gotta love
    sizeof('c')
    though, but that's a thread-hijack.

    Phil
    --
    > I'd argue that there is much evidence for the existence of a God.

    Pics or it didn't happen.
    -- Tom (/. uid 822)
    Phil Carmody, Jul 19, 2012
    #5
    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. Mantorok Redgormor

    sign magnitude, ones complement, two's complement

    Mantorok Redgormor, Oct 5, 2003, in forum: C Programming
    Replies:
    8
    Views:
    8,598
    Glen Herrmannsfeldt
    Oct 8, 2003
  2. sarathy

    1's complement and 2's complement

    sarathy, Aug 1, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,190
    Bo Persson
    Aug 2, 2006
  3. subramanian

    bitwise complement

    subramanian, Dec 20, 2006, in forum: C Programming
    Replies:
    11
    Views:
    543
    CBFalconer
    Dec 22, 2006
  4. sarathy
    Replies:
    22
    Views:
    2,347
    Bo Persson
    Aug 2, 2006
  5. Roberto Waltman

    2's complement vs. 1's complement vs. ...

    Roberto Waltman, Jun 9, 2011, in forum: C Programming
    Replies:
    4
    Views:
    1,355
    Michael Press
    Jun 14, 2011
Loading...

Share This Page