Skip to content

Beginner · IPv4 addressing · topic 11 of 57 · 25 min

Binary and decimal for networking

Explanation

Computers store everything as bits, ones and zeros. An IPv4 address is 32 bits, written as four groups of 8 bits called octets, and each octet is a number from 0 to 255. If you know the place values 128, 64, 32, 16, 8, 4, 2 and 1, you can convert any octet between decimal and binary in your head, and that skill is the base of all subnetting.

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.

Think of it like this: Think of a row of eight light switches on a wall. Each switch has a fixed value printed above it: 128, 64, 32, 16, 8, 4, 2 and 1. To show a number, you switch on the lights whose values add up to it. The pattern of on and off switches is the binary number.

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)1st2nd3rd4th5th6th7th8th
Place value1286432168421

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.

  1. 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.
  2. 168: take 128 (left 40), skip 64, take 32 (left 8), skip 16, take 8 (left 0), then 0, 0, 0. Answer 10101000.
  3. 10: skip 128, 64, 32 and 16, take 8 (left 2), skip 4, take 2 (left 0), skip 1. Answer 00001010.
  4. 255: every place value is taken. Answer 11111111.
  5. 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.

python
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)
Python can check your work. Learn to do it by hand first, because the CCNA exam gives you no calculator.

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.

Power2^12^22^32^42^52^62^72^82^92^10
Value2481632641282565121024

Common mistakes

Careful: Dropping the leading zeros. 10 in an octet is 00001010, not 1010. An octet always has exactly 8 bits, and if you drop the zeros, the bits move into the wrong places when you line up an address with its mask.
  • 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.
Tip: Practise a few conversions every day for a week: 224, 240, 248, 252 and 254 come up again and again in subnet masks. Soon you will see 11100000 and think 224 without adding anything.

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

Binary and decimal

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.

python
ip = "172.16.20.15"
print(".".join(format(int(octet), "08b") for octet in ip.split(".")))
Shows 10101100.00010000.00010100.00001111

Practice (0/4 done)

Task 1 · Code · Python

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".

Task 2 · Code · Python

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.

Task 3 · Code · Python

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".

Task 4 · Question

What is 11100000 in decimal? (This value appears in the mask 255.255.255.224.)

Quick quiz

Question 1

1.How many bits are in one octet?

Question 2

2.What is the largest number one octet can hold?

Question 3

3.What is 168 in binary?

Question 4

4.What is 00001111 in decimal?

Question 5

5.Why is 10 written as 00001010 in an IP address and not as 1010?

Read more (free)

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.

From Subnet masks, prefix length and wildcard masks

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.

From Network, broadcast and usable host range

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.

From IP addresses, subnets and CIDR

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.

From Subnetting a network into equal subnets

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

  1. 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.
  2. Module 3 exam: IPv4 addressing · 13 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Subnetting practice and IPv6. 57 topics left in the course before the final exam.

← Previous: The Cisco IOS command line