networking · intermediate · ~15 min

Compute network address from IP + prefix

Bitmask arithmetic on IPv4 addresses.

Challenge

Implement uint32_t network_address(uint32_t ip, int prefix_len) returning the network base by zeroing the host bits. ip is in host byte order; prefix_len is 0..32.

Starter code

#include <stdint.h>
uint32_t network_address(uint32_t ip, int prefix_len) { /* TODO */ return 0; }

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.