# Subnet in Binary

Subnetting is one of the most important pieces of knowledge an engineer must know. There’s quick ways to subnet but before learning the tricks, it’s important to know what is actually happening in binary.

An IPv4 address consists of 32 individual bits ranging between 0.0.0 – 255.255.255.255. The reason for this is due to 255 being the biggest decimal number that we’re able to express in binary with 8 bits. 1111 1111 = 255.

Everyone has probably heard of these once or twice. The classes of addresses. Class A, B, C.

Class A: 1.0.0.0 – 126.0.0.0 ( a /8)

Class B: 128.0.0.0 – 191.255.0.0 ( a /16)

Class C: 192.0.0.0 – 223.255.255.0 (a /24)

Now that we know what they are, lets throw them out the window! This is no longer used any longer and now we incorporate classless subnetting called VLSM – variable length subnet masks. However, we need to know the address ranges of the classful subnets due to some older protocols like RIP. (and old people)

## VLSM

VLSM means that the mask for example /24 or 255.255.255.0 determines the subnet size and no longer relies on the Class like A, B, or C to determine the subnet mask.

For instance you can have an address start with the class A IP address of 8.0.0.0 which by old standards would mean that you own all the IP address space of 8.0.0.0 – 8.255.255.255 (16,777216 addresses) but nowadays would be subnetted down through VLSM to maybe a /15 or 255.254.0.0. Which would give us an address range of 8.0.0.0 – 8.1.255.255.

## Binary

Lets break down the previous example with binary and how it actually works. We used an 8.0.0.0 with a subnet mask of 255.254.0.0 or in CIDR (classless inter-domain routing notation) a /15.

Binary is simply either a 0 or a 1. An IPv4 address has 32 bits ( a bit is a 0 or a 1) cut into four separate chunks called octets. Therefore, an IPv4 address is composed of bits in the pattern of:

```00000000.00000000.00000000.00000000 <-- 32 bits
This example equals 0.0.0.0```

```00001000.00000000.00000000.00000000 equals 8.0.0.0
11111111.11111110.00000000.00000000 equals the subnet mask of 255.254.0.0```

By looking at the above mask in binary, we can determine which bits are ‘locked’ and are non-permeable. Lets bold the Network ID with the Subnet Mask.

```00001000.00000000.00000000.00000000 <- 8.0.0.0
11111111.11111110.00000000.00000000 <- 255.254.0.0```

Going from the above, these bold bits are locked and can not be changed. To get the broadcast ID ( the last address in the subnet) we simply flip the unlocked bits.

```00001000.00000000.00000000.00000000 <- 8.0.0.0 Original
00001000.00000001.11111111.11111111 <- 8.1.255.255 Flipped```

By flipping our unlocked bits our broadcast ID is found of 8.1.255.255.

8.0.0.0 Network ID

One more example. 172.16.0.0/19

The /19 represents 19 locked bits meaning they can never change in this subnet. Lets show the the network ID and mask in binary.

```10101100.00010000.00000000.00000000 equals 172.16.0.0
11111111.11111111.11100000.00000000 equals /19 or 255.255.224.0```

If we flip the unlocked bits we get our broadcast

```10101100.00010000.00000000.00000000 equals 172.16.0.0
10101100.00010000.00011111.11111111 equals 172.16.31.255```

172.16.0.0 Network ID
255.255.224.0 Subnet ID