Example 1 — Bitwise operators on 12 and 10
12 is 1100 in binary and 10 is 1010.
#include <stdio.h>
int main(void) {
unsigned a = 12; /* 1100 */
unsigned b = 10; /* 1010 */
printf("a & b = %u\n", a & b);
printf("a | b = %u\n", a | b);
printf("a ^ b = %u\n", a ^ b);
printf("a << 2 = %u\n", a << 2);
printf("a >> 2 = %u\n", a >> 2);
printf("~a & 0xF = %u\n", ~a & 0xFu);
return 0;
} How It Works
1100 & 1010keeps only the shared bit:1000= 8.^keeps bits that differ:0110= 6.- Shifting left by 2 multiplies by 4; right by 2 divides by 4.
~aflips all 32 bits, so mask with0xFto see the low four bits (0011).
