NadirTools

IPv4 Subnetting Logic and Mathematical Proofs

2 min read

A deep dive into the Boolean logic, AND operations, and mathematical proofs behind subnetting.

Subnetting via Boolean Algebra

Subnetting relies heavily on the bitwise Logical AND operation. When a computer or router wants to determine if another destination IP is on its local subnet (meaning it can communicate directly without a gateway), it performs a bitwise AND between the destination IP address and its own [subnet mask](/tools/subnet-calculator/subnet-mask-reference-table).

The AND Rule Basics:

- `1 AND 1 = 1`

- `1 AND 0 = 0`

- `0 AND 1 = 0`

- `0 AND 0 = 0`

If the result of this AND operation matches the device's own network address, the destination is local. If it differs, the packet is sent to the default gateway.

Mathematical Proof of Subnet Counts

When you 'borrow' bits from the host portion of an IP address to create smaller subnets, the math follows strict powers of two.

Let `b` equal the number of borrowed bits.

Let `h` equal the original number of host bits before borrowing.

\[ \text{Created Subnets} = 2^b \]

\[ \text{Hosts per Subnet} = 2^{(h - b)} - 2 \]

Example Proof: Breaking a /24 into /27s

Imagine you start with a `/24` network (`192.168.1.0/24`). You need to create smaller `/27` subnets.

1. **Borrowed Bits (`b`)**: You are moving from a `/24` to a `/27`. That means you borrowed `27 - 24 = 3` bits.

2. **Subnets Created**: `2^3 = 8` subnets.

3. **Hosts per Subnet**: The new suffix is 27, leaving `32 - 27 = 5` host bits. So, `2^5 - 2 = 30` usable hosts per subnet.

Magic Number Technique

To quickly calculate subnet boundaries without binary math, network engineers use the 'Magic Number' method. The magic number is found by subtracting the interesting octet of the [subnet mask](/tools/subnet-calculator/subnet-mask-reference-table) from 256.

For a `/27` mask (`255.255.255.224`), the interesting octet is 224.

256 - 224 = 32

Therefore, your subnets will increment in blocks of 32: `.0`, `.32`, `.64`, `.96`, and so on.

Frequently Asked Questions

Q: What is the bitwise AND operation in networking?

It is a binary logic operation where the router compares an IP address and a subnet mask bit-by-bit. If both bits are 1, the result is 1; otherwise, it is 0. This reveals the underlying network address.

Q: What is the 'Magic Number' in subnetting?

The magic number is a shortcut for finding subnet boundaries. By subtracting the non-255 octet of a subnet mask from 256, you determine the block size or increment of the subnets.

Q: How many subnets are created if I borrow 4 bits?

Borrowing 4 bits creates 2^4, which equals 16 separate subnets.