Part III — Advanced: data structures, concurrency, networking
You can allocate memory and organize a program across files; this Part is where you build the structures that actually hold data — linked lists, trees, hash tables, and the Linux-kernel's intrusive-container trick — then add threads, atomics, and raw sockets on top. It ends with a thread pool and a multi-client chat server that exercises concurrency and networking together, not in isolation.
17. Bit manipulation and bit-fields
#include <stdio.h>
#include <stdint.h>
#define FLAG_READ (1u << 0)
#define FLAG_WRITE (1u << 1)
#define FLAG_EXECUTE (1u << 2)
int main(void) {
uint32_t perms = FLAG_READ | FLAG_WRITE; // set two flags
if (perms & FLAG_WRITE) printf("writable\n"); // test a flag
perms &= ~FLAG_WRITE; // clear a flag
perms ^= FLAG_EXECUTE; // toggle a flag
printf("%u\n", perms);
// Real-world example: this is exactly how chmod-style permission bits,
// network protocol flag fields, and hardware register access all work.
return 0;
}
Bit-fields let a struct pack sub-byte fields, common in wire-format and hardware-register structs (layout is implementation-defined in order, which is why real protocol code prefers explicit shifting/masking over bit-fields for anything that crosses machine boundaries):
typedef struct {
unsigned int is_admin : 1;
unsigned int level : 4; // 0-15
unsigned int reserved : 3;
} PermissionByte; // fits in 1 byte total