IPv4 addresses, binary & the IPv4 header
Convert IPv4 addresses to binary and identify network, host and special addresses.
By Invicly Tech, , in Computer Networking. About 10 minutes to read.
In short
An IPv4 address is 32 bits written as four decimal octets, such as 192.168.1.10. A prefix length splits it into a network part, shared by every host on the subnet, and a host part. Some ranges are reserved: private addresses for internal networks, loopback, and link-local.
Explained simply
Every device on an IP network needs an address so data can find it, just like every house needs a street address for post. An IPv4 address is a number made of 32 bits. To make it readable, we split it into four groups of 8 bits, called octets, and write each one as a decimal number from 0 to 255, like 192.168.1.10.
Each address has two parts. The first part names the network, and every device on the same local network shares it. The second part names the single device, or host, on that network. The prefix length, written like /24, says how many bits belong to the network part.
Some address ranges are special. Private ranges such as 10.x.x.x and 192.168.x.x can be used freely inside homes and companies, but they never appear on the public internet. 127.0.0.1 always means this computer itself. If you see 169.254.x.x, your device could not get an address from the network.
Every IPv4 packet starts with a header that holds the source and destination addresses, plus a few other fields. The most famous is the TTL (time to live): a counter that drops by one at every router, so a lost packet cannot circle the internet forever.
Press Play on a diagram to walk through it one step at a time.
- Private (RFC 1918)
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
- Loopback
- 127.0.0.0/8
- Link-local
- 169.254.0.0/16
- Seen when DHCP fails
- Carrier-grade NAT
- 100.64.0.0/10
- Multicast
- 224.0.0.0/4
- Unique on the whole internet
- Assigned by ISPs and registries
- Example: 8.8.8.8
- Reachable from anywhere if allowed
- Reused in millions of networks
- 10/8, 172.16/12, 192.168/16
- Needs NAT to reach the internet
- Dropped by ISPs if leaked
- Never leave the link or host
- 169.254.0.0/16 self-assigned
- 127.0.0.0/8 is this machine
- Not routed at all
In depth
Decimal and binary, quickly
Each octet has 8 bit positions with values 128, 64, 32, 16, 8, 4, 2 and 1. To convert a decimal number to binary, go left to right: if the value fits, write 1 and subtract it; otherwise write 0. For 172: 128 fits (44 left), 64 does not, 32 fits (12 left), 16 does not, 8 fits (4 left), 4 fits (0 left), then 0 and 0. So 172 is 10101100.
To go back, add up the values where there is a 1. Learn a few patterns by heart, because they appear in every subnet mask: 10000000 = 128, 11000000 = 192, 11100000 = 224, 11110000 = 240, 11111000 = 248, 11111100 = 252, 11111110 = 254 and 11111111 = 255. With these, most subnetting becomes simple arithmetic.
- Bit values: 128 64 32 16 8 4 2 1
- 172 = 10101100, 10 = 00001010
- Mask octets: 128, 192, 224, 240, 248, 252, 254, 255
- An octet is 0 to 255 because 8 bits give 256 values
Network part, host part and the AND operation
A host finds its own network by doing a bitwise AND between its address and its subnet mask: a bit is 1 in the result only if it is 1 in both. Because the mask is all 1s for network bits and all 0s for host bits, the AND keeps the network part and zeroes the host part.
For 192.168.1.10/24, the mask 255.255.255.0 keeps the first three octets, giving the network 192.168.1.0. Before sending any packet, a host ANDs the destination with its own mask too. If the result matches its own network, the destination is local and it sends directly using ARP. If not, it sends the packet to its default gateway. Every routing decision on a host starts with this one test.
- Step 1Own network
192.168.1.10 AND 255.255.255.0 = 192.168.1.0
- Step 2Destination network
192.168.1.200 AND 255.255.255.0 = 192.168.1.0
- Step 3Compare
Same result: the destination is local
- Step 4Deliver
ARP for 192.168.1.200 and send directly
Classes, and the special ranges you must recognise
Before 1993, addresses were split into fixed classes by their first octet: class A (1 to 126) with a /8 network, class B (128 to 191) with /16, class C (192 to 223) with /24, class D (224 to 239) for multicast and class E (240 to 255) reserved. CIDR replaced classes, but people still say class C for a /24.
Beyond the private ranges, learn these. 0.0.0.0 means this host or any address, and 0.0.0.0/0 is the default route. 255.255.255.255 is the limited broadcast, which never crosses a router. 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24 are reserved for documentation, so use them in examples instead of real addresses. 100.64.0.0/10 is shared space for carrier-grade NAT inside ISPs.
- 0.0.0.0/0: default route; 0.0.0.0 in a listen address means all interfaces
- 255.255.255.255: limited broadcast
- 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24: documentation
- 224.0.0.0/4 multicast, 240.0.0.0/4 reserved
The IPv4 header field by field
The header is at least 20 bytes. Version is 4. IHL (header length) counts 32-bit words, so 5 means 20 bytes and the maximum, 15, means 60. DSCP and ECN carry priority and congestion marks. Total Length is the whole packet size, up to 65,535 bytes. Identification, Flags and Fragment Offset handle fragmentation; the DF (don't fragment) flag forbids routers from splitting the packet, and MF (more fragments) marks all fragments but the last.
TTL starts at 64 on Linux and macOS, 128 on Windows and 255 on many routers, and each router subtracts one. Protocol names the payload: 1 ICMP, 6 TCP, 17 UDP, 47 GRE, 50 ESP. The header checksum covers only the header and is recomputed at every hop because TTL changes. Then come the 4-byte source and destination addresses and any options.
- 1Row 1
Version 4, IHL 4, DSCP 6, ECN 2, Total Length 16 bits
- 2Row 2
Identification 16, Flags 3, Fragment Offset 13 bits
- 3Row 3
TTL 8, Protocol 8, Header Checksum 16 bits
- 4Row 4
Source address, 32 bits
- 5Row 5
Destination address, 32 bits
- 6Options
0 to 40 bytes, rarely used
Learn it in 4 short steps
About 40m each. Track them with a timer in your plan.
- 1Convert between decimal and binary octets~40m
- 2Split address into network and host~40m
- 3Recognise private and special ranges~40m
- 4Read key IPv4 header fields~40m
Key points
- 192.168.1.10 in binary is 11000000.10101000.00000001.00001010.
- There are about 4.3 billion IPv4 addresses, which is why NAT exists.
- Private addresses are not routed on the public internet.
- Key header fields: TTL, protocol, source and destination address.
Key terms
Tap a card to flip it. 0/4 known
Examples
import ipaddress
ip = ipaddress.ip_address('192.168.1.10')
print(ip.is_private, format(int(ip), '032b'))
print(ipaddress.ip_address('8.8.8.8').is_global) # TrueReading an IPv4 header in tcpdump
Run tcpdump with -v while pinging 1.1.1.1. A reply line shows: tos 0x0, ttl 57, id 0, offset 0, flags [none], proto ICMP (1), length 84. Step 1: length 84 is 20 bytes of IP header plus 64 bytes of ICMP (8 header + 56 data, ping's default). Step 2: proto 1 is ICMP. Step 3: ttl 57 suggests the server started at 64 and the reply crossed 7 routers. Step 4: on your outgoing request, flags [DF] shows Linux set don't fragment, which it does for path MTU discovery.
sudo tcpdump -i any -nn -v -c 4 icmp &
ping -c 2 1.1.1.1
# IP (tos 0x0, ttl 64, id 4242, offset 0, flags [DF], proto ICMP (1), length 84)
# 10.0.0.5 > 1.1.1.1: ICMP echo request, id 7, seq 1, length 64Converting and ANDing by hand, checked in the shell
Is 172.20.33.9 in the same /20 network as 172.20.40.1? Step 1: only the third octet is interesting, because a /20 mask is 255.255.240.0. Step 2: 33 is 00100001 and 40 is 00101000. Step 3: AND each with 240 (11110000): both give 00100000 = 32. Step 4: both are in 172.20.32.0/20, so they are on the same subnet. The shell can check the binary for you.
for n in 33 40 240; do echo "$n = $(echo "obase=2; $n" | bc)"; done
echo $(( 33 & 240 )) $(( 40 & 240 )) # 32 32 -> same /20
ipcalc 172.20.33.9/20 | grep -E 'Network|HostMin|HostMax'Quick reference
- Bit values per octet: 128 64 32 16 8 4 2 1
- Private: 10.0.0.0/8, 172.16.0.0/12 (172.16 to 172.31), 192.168.0.0/16
- 169.254.x.x = DHCP failed; 127.0.0.1 = loopback; 100.64.0.0/10 = carrier-grade NAT
- Network address = IP AND mask
- Default TTL: Linux 64, Windows 128, many routers 255
- IP protocol numbers: ICMP 1, TCP 6, UDP 17, GRE 47, ESP 50, OSPF 89
- Use 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24 in docs and examples
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Convert 10.172.3.200 to binary and convert 11000000.10101000.00001010.11111110 back to decimal.
- Exercise 2medium
For each address, say what kind it is: 100.72.4.9, 169.254.12.7, 203.0.113.50, 172.15.0.1, 239.255.255.250.
- Exercise 3hard
Write a Python function that returns True if two IPv4 addresses are on the same subnet for a given prefix length, using only integer bit operations (no ipaddress module).
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Why are there only about 4.3 billion IPv4 addresses?
- Because an address is 32 bits, and 2 to the power 32 is 4,294,967,296. Large blocks are also reserved, so even fewer are usable on the internet.
- Can two devices have the same IP address?
- Not on the same network; that is an IP conflict and both will lose traffic. Private addresses are reused in different networks all the time, because NAT keeps them apart.
- What is the difference between 0.0.0.0 and 127.0.0.1?
- 127.0.0.1 is the loopback address: traffic never leaves your machine. 0.0.0.0 as a server listen address means listen on every interface, and as a route it means any destination.