A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
Bitwise operators work on the individual bits of integer values. AND, OR, XOR, NOT, left shift, right shift. They are the tools of flag management, permission masks, network protocol parsing, embedded register configuration, and cryptographic primitives. If you are writing systems code, you will use them daily. If you are writing application code, you will still encounter them in every header file that defines constants like O_RDONLY | O_CREAT.
#include <stdio.h>void show_bits(unsigned int val, int bits) {for (int i = bits - 1; i >= 0; i--) {printf("%d", (val >> i) & 1);if (i % 4 == 0 && i > 0) printf(" ");}printf("\n");}int main(void) {unsigned int a = 0b11001010;unsigned int b = 0b10100110;printf("a: "); show_bits(a, 8);printf("b: "); show_bits(b, 8);printf("a & b: "); show_bits(a & b, 8);printf("a | b: "); show_bits(a | b, 8);printf("a ^ b: "); show_bits(a ^ b, 8);printf("~a: "); show_bits(~a & 0xFF, 8);printf("a << 2: "); show_bits((a << 2) & 0xFF, 8);printf("a >> 2: "); show_bits(a >> 2, 8);return 0;}
#include <stdio.h>#define PERM_READ 0x04#define PERM_WRITE 0x02#define PERM_EXECUTE 0x01int main(void) {unsigned char perms = PERM_READ | PERM_WRITE;printf("Permissions: 0x%02X\n", perms);printf("Can read: %s\n", (perms & PERM_READ) ? "yes" : "no");printf("Can write: %s\n", (perms & PERM_WRITE) ? "yes" : "no");printf("Can execute: %s\n", (perms & PERM_EXECUTE) ? "yes" : "no");perms &= ~PERM_WRITE;printf("\nAfter removing write: 0x%02X\n", perms);printf("Can write: %s\n", (perms & PERM_WRITE) ? "yes" : "no");return 0;}
OR sets flags. AND tests flags. AND with NOT clears flags. XOR toggles flags. These four idioms cover every flag manipulation you will ever need. The Unix permission system, the Linux kernel's feature flags, and every hardware register interface use this pattern. Learn it until it is muscle memory.
Right-shifting a negative signed integer is implementation-defined. Some compilers do arithmetic shift (sign-extending), others do logical shift (zero-filling). For portable bit manipulation, use unsigned types. Always.
Bitwise operators are the bridge between your C code and the hardware's binary reality. Flags, masks, shifts, and tests. Four patterns. Infinite applications. Next: the comma operator, the most misunderstood operator in C, and the only one that is almost never what you think it is.
Finish the lesson once you have worked through the material. This awards ★ 30 XP.