Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Bitwise complement

Reply
Thread Tools

Bitwise complement

 
 
Martin Kojtal
Guest
Posts: n/a
 
      07-18-2012
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 &= ~((UINTFIRST_BIT_MASK);

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

MartinK
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-18-2012
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 &= ~((UINTFIRST_BIT_MASK);
>
> I am inclined to cast the result of bitwise complement: (UINT(~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 (UINT 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


 
Reply With Quote
 
 
 
 
ImpalerCore
Guest
Posts: n/a
 
      07-18-2012
On Jul 18, 4:56*am, Martin Kojtal <koj...@gmail.com> 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 &= ~((UINTFIRST_BIT_MASK);
>
> I am inclined to cast the result of bitwise complement: (UINT(~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 (UINT cast in the definition of FIRST_BIT_MASK,
i.e. #define FIRST_BIT_MASK ((UINT0xA0).

Best regards,
John D.
 
Reply With Quote
 
Les Cargill
Guest
Posts: n/a
 
      07-18-2012
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 &= ~((UINTFIRST_BIT_MASK);
>


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

> I am inclined to cast the result of bitwise complement: (UINT(~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

 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      07-19-2012
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2's complement vs. 1's complement vs. ... Roberto Waltman C Programming 4 06-13-2011 11:26 PM
bitwise complement subramanian C Programming 11 12-22-2006 02:38 AM
1's complement and 2's complement sarathy C++ 22 08-02-2006 05:53 PM
1's complement and 2's complement sarathy C Programming 20 08-02-2006 05:53 PM
sign magnitude, ones complement, two's complement Mantorok Redgormor C Programming 8 10-07-2003 11:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57