set bit in c code example
Example 1: toggling setting clearing checking changing a bit in c
// Setting a bit
number |= 1UL << n;
// Clearing a bit
number &= ~(1UL << n);
// Toggling a bit
number ^= 1UL << n;
// Checking a bit
bit = (number >> n) & 1U;
// Changing the nth bit to x
number ^= (-x ^ number) & (1UL << n);
Example 2: opération bit à bit c
unsigned a = 0xF0F0;
unsigned b = 0x00FF;
unsigned c = a & b; /* c == 0000000011110000 soit 0x00F0 */