5.2.7 Bit Instructions
Wherever possible, the MPLAB XC8 C Compiler will attempt to use bit instructions, even on non-bit integer values. For example, when using a bitwise operator and a mask to alter a bit within an integral type, the compiler will check the mask value to determine if a bit instruction can achieve the same functionality.
unsigned int foo;
foo |= 0x40;
will produce the instruction:
bsf _foo,6
To set or clear individual bits within integral type, the following macros could be used:
#define bitset(var, bitno) ((var) |= 1UL << (bitno))
#define bitclr(var, bitno) ((var) &= ~(1UL << (bitno)))
To perform the same operation on foo
as above, the
bitset()
macro could be employed as follows:
bitset(foo, 6);