TL;DR
An IP address is the number that identifies a device on an IP network, the address that a packet carries so that routers know where to send it. In the common version, IPv4, the address is a 32-bit binary number, written as four decimal groups such as 192.168.1.37. Its structure lets routers forward a packet by looking at only part of the address.
Builds on: Binary Numbers (Bits and Bytes) · Leads to: Packet Switching, TCP, Network Address Translation
Where it's used
Every connection on the internet. When your phone loads a web page over TCP, the data is cut into packets, and each packet it sends carries the phone's IP address as the source and the server's IP address as the destination. The server's reply carries the two addresses the other way round. Without these addresses the reply would have nowhere to go.
Home and office networks. A home router usually gives each laptop and phone an address in a private range, such as 192.168.1.2 to 192.168.1.254. Those addresses only mean something inside the house. The router uses network address translation to share one public address with everyone in the house.
Routing between networks. Internet service providers and large companies run routers that sit at the network layer of a protocol stack, read the destination address of each packet and pick the next hop. They do this for a very large number of packets every second, and the structure of the address is what makes that lookup fast enough.
How it works
Take the address 192.168.1.37 and follow how a device turns it into bits. The four groups are the four bytes of a 32-bit number, and each group is written in ordinary decimal.
-
Read the four groups. The address has four groups separated by dots: 192, 168, 1 and 37. Each group is one byte, which holds a whole number from 0 to 255. Check: all four groups are at most 255.
-
Convert each group to 8 bits. Use the binary place values 128, 64, 32, 16, 8, 4, 2, 1. For 192, , so the byte is 11000000. For 168, , so the byte is 10101000. Check: each byte has eight digits.
-
Convert the last two groups. For 1, the byte is 00000001. For 37, , so the byte is 00100101. Check: each place used adds up to the number.
-
Join the bytes into one 32-bit number. The full address is 11000000 10101000 00000001 00100101. Check: there are four bytes, so 32 bits in total.
-
Split it into network and host. The address is divided at a boundary chosen by the network. In 192.168.1.37/24, the first 24 bits (the first three groups, 192.168.1) name the network, and the last 8 bits (the 37) name the device on that network. A router that only needs to know the network can forward on the first 24 bits.
-
Compare two addresses. Take 192.168.1.200. Its first three groups are also 192.168.1, so it is on the same network as 192.168.1.37. Now take 192.168.2.37. Its third group differs, so it is on a different network and must go through a router.
flowchart LR
A["Address 192.168.1.37"] --> B["Write each group as 8 bits"]
B --> C["Join into 32 bits"]
C --> D["Split at the prefix length"]
D --> E["Network part: routing decision"]
D --> F["Host part: which device"]
The math
An IPv4 address is a 32-bit string , where each is 0 or 1. Its value is
It is written as four decimal numbers , where each is the value of one 8-bit group, so .
The symbol means "add up over the range shown", and is the place value of bit in binary, as in Binary Numbers (Bits and Bytes). The subscript counts positions from the right, starting at 0.
Step 2 in symbols: one group to bits
For a group , the bits satisfy . For , the bits are , , and the rest 0, because .
Step 4 in symbols: the 32-bit value
The four groups give the 32-bit value
For 192.168.1.37 this is . Check the arithmetic one operation at a time: , , so the sum is .
Step 5 in symbols: network and host parts
A prefix length says how many leading bits name the network. The network part is the first bits, and the host part is the remaining bits. For the host part has bits, so each network has possible addresses, although a few are reserved. Two addresses are on the same network exactly when their first bits agree.
Step 6 in symbols: the comparison
For 192.168.1.37 and 192.168.1.200 the first 24 bits are both 192.168.1, so they match. For 192.168.2.37 the first 24 bits are 192.168.2, which differs in the third group, so they do not match.
Worked examples
Convert the address 10.0.0.5 to a 32-bit binary string.
- Convert 10 to 8 bits. , so the byte is 00001010.
- Convert 0 to 8 bits. The byte is 00000000.
- Convert the next 0. The byte is 00000000.
- Convert 5 to 8 bits. , so the byte is 00000101.
- Join the four bytes: 00001010 00000000 00000000 00000101.
Check: the bits in the first byte give , and the last byte gives .
A host with address 172.16.4.10 and prefix length 16 wants to send to 172.16.200.7 and to 172.17.4.10. Which destination is on the same network?
- With , the network part is the first two groups, 172.16.
- The destination 172.16.200.7 has first two groups 172.16. The network part matches, so it is on the same network.
- The destination 172.17.4.10 has first two groups 172.17. The second group differs from 16, so the network part does not match.
- So the first destination is reached directly, and the second needs a router.
Check: a larger prefix length would make the network smaller. With the first destination would be on a different network, because its third group is 200, not 4.
A laptop has address 192.168.0.12 with prefix length 24. The router is at 192.168.0.1. A phone at 192.168.0.40 sends a message to a server at 93.184.216.34.
- The laptop checks the destination. Its first three groups are 192.168.0, and the server's first three groups are 93.184.216. These differ, so the server is not on the local network.
- The laptop sends the packet to the router, its default gateway at 192.168.0.1.
- The router reads the destination address 93.184.216.34 and forwards it toward the internet. It is the device that can reach addresses outside the house.
- The router replaces the sending device's private address with its own public address when the packet leaves, and records the mapping so the reply can be returned to the right device.
- Check the local rule: the phone at 192.168.0.40 is on the same 192.168.0 network as the laptop, so the two can talk directly without the router.
Try it: a device at 192.168.0.40 with prefix length 24 sends to 192.168.1.5. Is the destination on the same network? Answer: no. The third groups differ, 0 against 1, so the packet goes through the router.
Common mistakes and tradeoffs
It is tempting to think an IP address names a particular computer forever. In fact it names a network interface, and the address can change when a device moves networks or when a provider reassigns it. The address alone also does not pick the program that receives the data: transport protocols such as TCP and UDP add port numbers on top of it for that job. A laptop can have one address at home and a different one at a café.
It is tempting to think a private address such as 192.168.1.37 can be reached from anywhere on the internet. In fact such addresses are only meaningful inside their own network, and the router shares one public address with them through Network Address Translation. Two different houses can each have a device at 192.168.1.37 without conflict.
It is tempting to think the 32 bits are just a serial number. In fact the structure matters. Routers forward on the network part, so addresses are assigned in blocks that share a prefix. Without that structure, every router would need an entry for every device.
When to use it: whenever a packet must reach a specific device on a network, and routers need to decide where to send it. When not to use it: when you need a name a person can remember or a stable identity that survives a move. Use a domain name for the first, which the DNS maps to an address, and a higher-level identifier for the second. Alternative: IPv6 uses 128-bit addresses, which gives far more room. IPv4 has about 4.3 billion addresses in total, which is not enough for every device, so networks rely on private ranges and address translation to share them.
History
In the early 1970s Vinton Cerf and Robert Kahn designed the basic ideas of the internet, including a way to send messages as packets with addresses that any network could read. Their 1974 paper set out the design. The Internet Protocol was specified in RFC 791 in 1981, and the 32-bit IPv4 address became the standard. As the internet grew, the pool of unused IPv4 addresses ran out, and the IETF defined the 128-bit IPv6 address in the late 1990s. Both versions are still in use, because switching the whole internet to IPv6 has taken many years.
Recap
- An IP address identifies a device on an IP network, and packets carry source and destination addresses.
- An IPv4 address is 32 bits, written as four groups from 0 to 255, each one byte.
- A prefix length splits the address into a network part and a host part.
- Two addresses are on the same network when their network parts match, and a router is needed otherwise.
- Read next: Packet Switching, which explains how packets with these addresses travel, and Network Address Translation, which lets many private addresses share one public one. The end-to-end principle explains why the network keeps this forwarding job simple.
Check yourself
QuestionConvert the address 192.168.0.255 to its four 8-bit groups.Show answer
192 is 11000000, 168 is 10101000, 0 is 00000000, and 255 is 11111111, because . The full address is 11000000 10101000 00000000 11111111.
QuestionAn IPv4 address with prefix length 24 is 10.20.30.40. What are its network part and its host part?Show answer
The network part is the first 24 bits, which are the first three groups, so it is 10.20.30. The host part is the last 8 bits, the group 40, so the device is host number 40 on that network.
QuestionWhy does a router only need the network part of an address to forward most packets?Show answer
Routers keep a table of networks rather than of individual devices. Each entry says which next hop leads to a whole network. Once the network part is matched, the router forwards the packet, and the final host part is handled inside that network. Without this, the table would need one entry for every device.
QuestionA device has address 172.20.7.9 and prefix length 16. Is the device at 172.20.100.2 on the same network? Explain using the bits that matter.Show answer
Yes. With prefix length 16, the first 16 bits are the first two groups, 172.20. The other device's first two groups are also 172.20, so the network parts match. The third group, 100 versus 7, is part of the host part and does not decide the network.