Bitwise AND in IP Address Network Prefix Extraction

The bitwise AND operation is the fundamental mathematical mechanism used by routers and networking devices to extract the network prefix (or network address) from an IP address. By comparing the binary representation of an IP address with its corresponding subnet mask, the bitwise AND operation deterministically preserves the network portion of the address while setting all host bits to zero. This article explains how this binary operation works, its step-by-step mathematical logic, and why it is essential for IP routing and subnetting.

How the Bitwise AND Logic Operates

In binary logic, the AND operation evaluates two bits and outputs 1 only if both input bits are 1. If either input bit—or both—is 0, the output is 0.

The truth table for a bitwise AND operation is: * 1 AND 1 = 1 * 1 AND 0 = 0 * 0 AND 1 = 0 * 0 AND 0 = 0

When applied to networking, the bitwise AND operation compares each corresponding bit of a 32-bit IPv4 address against a 32-bit subnet mask.

The Role of the Subnet Mask

A subnet mask consists of a contiguous sequence of binary 1s representing the network portion (the prefix), followed by a contiguous sequence of binary 0s representing the host portion.

Because of the rules of the AND operation: 1. Any bit ANDed with 1 retains its original value: The network bits of the IP address pass through unchanged because the corresponding mask bits are 1. 2. Any bit ANDed with 0 becomes 0: The host bits of the IP address are cleared to 0 because the corresponding mask bits are 0.

Step-by-Step Example of Network Prefix Extraction

Consider an IP address of 192.168.1.130 with a standard /24 subnet mask (255.255.255.0).

1. Convert to Binary

2. Apply the Bitwise AND Operation

  11000000.10101000.00000001.10000010  (IP: 192.168.1.130)
& 11111111.11111111.11111111.00000000  (Mask: 255.255.255.0)
-------------------------------------
  11000000.10101000.00000001.00000000  (Result: 192.168.1.0)

3. Resulting Network Prefix

Converting the binary result back to dotted-decimal notation produces 192.168.1.0. The host-specific portion (.130) is masked out, leaving only the base network address.

Why This Operation Is Crucial