I’ve used the following macro in C:
#define test_bit(_n,_p) !! ( _n & ( 1u << _p))
I’ve studied the macro but I need to determine the use of the double negative in the macro, and how it behaves differently from the macro definition :
#define test_bit(_n,_p) ( _n & ( 1u << _p))
Think about what happens when you test a specific bit:
If you left it like that, your result would be the bitmask itself.
Now consider the double (logical, not bitwise) negation of that, which is not the same as doing nothing:
In other words,
!!4gives you1rather than4and this guarantees that you will get a0/1truth value rather than a0/whatever-the-bitmask-wasvalue.The following program shows this in action:
and it outputs:
as expected.