[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [avr-gcc-list] C coding question
From: |
Eric Weddington |
Subject: |
RE: [avr-gcc-list] C coding question |
Date: |
Tue, 10 Oct 2006 16:29:10 -0600 |
> -----Original Message-----
> From:
> address@hidden
> [mailto:address@hidden
> org] On Behalf Of Paulo Marques
> Sent: Monday, October 09, 2006 4:39 AM
> To: larry barello
> Cc: address@hidden
> Subject: Re: [avr-gcc-list] C coding question
>
> larry barello wrote:
> > [...]
> > So I said:
> >
> > (bSomeBool?1:0) ^ ((SomeBitMask & SomeVariable)?1:0))
>
> Since no one else made this comment, I just wanted to point
> out that you
> can write that as:
>
> (bSomeBool ^ (!!(SomeBitMask & SomeVariable)))
>
> "!!" is often used to convert an integer value into a logical (0/1)
> value. Maybe it is easier to the compiler than "?1:0".
I would question the "often used" phrase; this is the first I've heard of it
in 15 years of programming in C.
If you want a logical (boolean) XOR operation, you can write the expression
simply:
#include <stdbool.h>
...
(bool)(bSomeBool != (bool)(SomeBitMask & SomeVariable))
Remember that the type of an equality/non-equality operator is another
boolean value. Be sure to typecast the bitwise and operation to a bool value
so the != operator is comparing values of the same type.