# IPv4 Addresses and CIDR Notation — Cloud Networking: VPCs, Subnets & Peering

Source: https://www.skillbyai.com/en/cloud-networking/cn-ip-cidr

> Read a CIDR block like 10.0.0.0/16, work out how many addresses it holds and test whether an IP falls inside it.

## An address is 32 bits; the prefix says how many are fixed

An IPv4 address is a 32-bit number written as four decimal *octets*, such as `10.0.42.7`. **CIDR notation** adds a slash and a prefix length: `10.0.0.0/16` means the first 16 bits are fixed (the *network* part) and the remaining 16 bits are free (the *host* part). The block therefore contains 2^(32-16) = 65,536 addresses, from `10.0.0.0` to `10.0.255.255`. Each extra bit of prefix halves the block: a `/24` has 256 addresses, a `/28` only 16. The **netmask** is the same idea written as an address: `/16` is `255.255.0.0`. Students often think a larger prefix number means a larger network; it is the opposite, because more bits are locked. Almost every cloud networking decision you make (VPC size, subnet size, route destinations, firewall sources) is written as a CIDR block, so being fluent here saves hours of confusion later.

## Exploring a block with Python's ipaddress module

The standard library does the bit arithmetic for you, which is handy for checking plans before you click anything in a console.

```python
import ipaddress

vpc = ipaddress.ip_network("10.0.0.0/16")
print("addresses:", vpc.num_addresses)
print("netmask:  ", vpc.netmask)
print("first/last:", vpc[0], vpc[-1])
print(ipaddress.ip_address("10.0.42.7") in vpc)
print(ipaddress.ip_address("10.1.0.1") in vpc)
```

Output:

```
addresses: 65536
netmask:   255.255.0.0
first/last: 10.0.0.0 10.0.255.255
True
False
```

## Quick mental maths

Addresses in a block = 2^(32 - prefix). Memorise a few anchors: /16 = 65,536, /20 = 4,096, /24 = 256, /26 = 64, /28 = 16. Moving one step left or right just doubles or halves the count.

**Quiz:** How many IPv4 addresses does the block 10.0.0.0/20 contain?

- [x] 4,096
- [ ] 1,024
- [ ] 65,536
- [ ] 256

*Answer:* 4,096. 32 - 20 = 12 free host bits, and 2^12 = 4,096 addresses.
