Beginner · IPv4 addressing · topic 11 of 57 · 25 min
Binary and decimal for networking
Explanation
Why computers use binary
Inside a computer, a router or a switch, information is stored and sent as signals that are either on or off: a voltage is high or low, a light pulse in a fibre is bright or dark. A single on or off value is called a bit (short for binary digit). We write on as 1 and off as 0. Binary is the number system that uses only these two digits.
We humans count in decimal, base 10, because we have ten fingers. Each place in a decimal number is worth ten times the place to its right: ones, tens, hundreds. Binary works the same way, but each place is worth two times the place to its right.
An octet: 8 bits, 0 to 255
Network devices group bits in sets of 8. A group of 8 bits is called a byte, and in networking we usually call it an octet (octo means eight). An IPv4 address is 32 bits long, which is 4 octets. Each octet is written as a normal decimal number, and the four numbers are separated by dots: 192.168.10.1. This is called dotted decimal notation.
| Bit position (left to right) | 1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
With all eight bits off you get 00000000, which is 0. With all eight on you get 11111111, which is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. So one octet can hold 256 different values, from 0 to 255. That is why you will never see 256 or 300 in a valid IPv4 address.
Decimal to binary: the subtraction method
Go through the place values from left to right. If the number is at least as big as the place value, write 1 and subtract that value. If not, write 0 and move on. Keep going until all eight places are filled.
- 192: 192 is at least 128, write 1, left 64. 64 is at least 64, write 1, left 0. The other six places are 0. Answer 11000000.
- 168: take 128 (left 40), skip 64, take 32 (left 8), skip 16, take 8 (left 0), then 0, 0, 0. Answer 10101000.
- 10: skip 128, 64, 32 and 16, take 8 (left 2), skip 4, take 2 (left 0), skip 1. Answer 00001010.
- 255: every place value is taken. Answer 11111111.
- 172: take 128 (left 44), skip 64, take 32 (left 12), skip 16, take 8 (left 4), take 4 (left 0), then 0, 0. Answer 10101100.
Binary to decimal: add the place values
Write the place values above the bits and add up the values that sit above a 1. For 11000000, only 128 and 64 are on, so it is 192. For 10101100, the ones sit under 128, 32, 8 and 4, so it is 172. For 00000001, only the 1 place is on, so it is 1.
So the address 192.168.10.1 in binary is 11000000.10101000.00001010.00000001. Network engineers write the dots between octets so the bits are easier to read.
print(bin(192)) # 0b11000000 (the 0b prefix means binary)
print(format(10, "08b")) # 00001010 (8 digits, padded with zeros)
print(int("10101100", 2)) # 172 (read a string as base 2)Powers of two you should know
Subnetting is mostly about powers of two: 2 multiplied by itself a number of times. Learn this short list by heart, it will save you time in every later topic.
| Power | 2^1 | 2^2 | 2^3 | 2^4 | 2^5 | 2^6 | 2^7 | 2^8 | 2^9 | 2^10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Value | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024 |
Common mistakes
- Starting from the wrong end: the leftmost bit is worth 128, the rightmost is worth 1.
- Thinking an octet goes up to 256. It holds 256 values, but they run from 0 to 255.
- Reading the binary string as a decimal number. 11000000 is "one one zero zero zero zero zero zero", not eleven million.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Binary and decimal for networking · 1/8
Welcome. Before we can understand IP addresses, we need to read binary, the language of ones and zeros that every computer uses.
Prefer a video by a teacher? Find free videos on “binary to decimal conversion for networking ccna” on YouTube ↗
In real life: A support engineer reads a packet capture
A network support engineer at a Bengaluru BPO opens a packet capture in Wireshark to find out why some agents cannot reach the call server. The tool shows the source address both as 172.16.20.15 and as raw bits in the header. Because she can turn 172 into 10101100 and back in her head, she quickly matches the bits with the mask and sees that one agent PC has a wrong address in a different subnet. A five-minute fix instead of a two-hour hunt.
ip = "172.16.20.15"
print(".".join(format(int(octet), "08b") for octet in ip.split(".")))Practice (0/4 done)
Write to_binary(n) that turns a number from 0 to 255 into an 8-digit binary string, using the subtraction method with the place values 128, 64, 32, 16, 8, 4, 2, 1. Do not use bin() or format() for this one: the point is to practise the method. Example: to_binary(192) returns "11000000".
Write to_decimal(bits) that turns an 8-character string of 0s and 1s into its decimal number by adding the place values under each 1. Example: to_decimal("11000000") returns 192.
Write ip_to_binary(ip) that turns a dotted decimal IPv4 address into dotted binary, 8 bits per octet. Here you may use format(number, "08b"). Example: ip_to_binary("192.168.10.1") returns "11000000.10101000.00001010.00000001".
What is 11100000 in decimal? (This value appears in the mask 255.255.255.224.)
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- IPv4 address structure and special rangesIPv4 addressing · same module
- Subnet masks, prefix length and wildcard masksIPv4 addressing · same module
- Network, broadcast and usable host rangeIPv4 addressing · same module
- IP addresses, subnets and CIDRCyber Security: from zero to SOC analyst · Networking for security
- Subnetting a network into equal subnetsSubnetting practice and IPv6
- IPv6 addresses: format and shortening rulesSubnetting practice and IPv6
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
How many bits are in one octet?
Answer: 8
An octet is 8 bits. An IPv4 address has 4 octets, so 32 bits.
What is the largest number one octet can hold?
Answer: 255
All 8 bits on: 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255.
What is 168 in binary?
Answer: 10101000
168 = 128 + 32 + 8, so the bits for 128, 32 and 8 are on: 10101000.
What is 00001111 in decimal?
Answer: 15
The four bits on the right are on: 8 + 4 + 2 + 1 = 15.
Why is 10 written as 00001010 in an IP address and not as 1010?
Answer: Because every octet is always exactly 8 bits
An octet always has 8 bit positions, so the leading zeros keep every bit in its right place.
What is the dotted decimal mask for /27?Related
Answer: 255.255.255.224
/27 = 24 + 3: three ones in the last octet = 128 + 64 + 32 = 224.
What is the network address of 192.168.5.130/25?Related
Answer: 192.168.5.128
/25 has a block size of 128. 130 is in 128 to 255, so the network is 192.168.5.128.
Which address is private?Related
Answer: 172.20.1.5
172.16.0.0/12 covers 172.16.x.x to 172.31.x.x, so 172.20.1.5 is private; 172.32.1.5 is not.
Each subnet must hold 25 hosts. Which prefix wastes the fewest addresses?Related
Answer: /27
/27 gives 30 usable hosts. /28 gives only 14, and /26 wastes more.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 4 more topics in “IPv4 addressing” before its exam: IPv4 address structure and special ranges, Subnet masks, prefix length and wildcard masks, Network, broadcast and usable host range, Validating addresses and checking the same subnet.
- Module 3 exam: IPv4 addressing · 13 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Subnetting practice and IPv6. 57 topics left in the course before the final exam.