Kahibaro
Discord Login Register

2.3 Decimal ↔ Binary Conversion

Understanding Decimal and Binary Conversion

In networking, devices represent all information as binary. Humans prefer decimal. Being able to move between these two views is essential, especially for topics like IP addressing and subnetting that appear later in the course. This chapter focuses only on how to convert between decimal and binary, and how to read these representations with confidence.

Place Values in Decimal and Binary

Before converting, you must understand place values.

In the decimal system, each position is a power of 10. From right to left, the places are:

Position (from right)Power of 10Place value
0$10^0$1
1$10^1$10
2$10^2$100
3$10^3$1000

For example, the decimal number 472 means:
$$4 \times 100 + 7 \times 10 + 2 \times 1$$

In the binary system, each position is a power of 2. From right to left, the places are:

Position (from right)Power of 2Place value
0$2^0$1
1$2^1$2
2$2^2$4
3$2^3$8
4$2^4$16
5$2^5$32
6$2^6$64
7$2^7$128

So a binary number like 1011 means:
$$1 \times 8 + 0 \times 4 + 1 \times 2 + 1 \times 1$$
which equals 11 in decimal.

Important:
Decimal uses powers of 10.
Binary uses powers of 2.
Each binary digit (bit) is worth either its place value or zero.

Converting Binary to Decimal

To convert from binary to decimal, you expand the binary number using powers of 2, then add the values where there is a 1 bit.

Step by step method

  1. Write down the binary number.
  2. Label each bit with its place value, starting from the right with $2^0$.
  3. For each bit that is 1, write down its place value.
  4. Add all those place values to get the decimal result.

Example 1: Convert $1011_2$ to decimal

Number: 1011

Place values:

BitPlace (power)ValueContribution
1$2^3$88
0$2^2$40
1$2^1$22
1$2^0$11

Add the contributions: $8 + 0 + 2 + 1 = 11$

So $1011_2 = 11_{10}$.

Example 2: Convert $110010_2$ to decimal

Number: 110010

Label with powers of 2, from right to left:

BitPowerValueContribution
0$2^0$10
1$2^1$22
0$2^2$40
0$2^3$80
1$2^4$1616
1$2^5$3232

Add: $32 + 16 + 2 = 50$

So $110010_2 = 50_{10}$.

Rule for binary to decimal:
Decimal value = sum of (bit value) × (corresponding power of 2).
In symbols:
$$N_{10} = \sum_{i=0}^{k} b_i \times 2^i$$
where each $b_i$ is either 0 or 1.

Converting Small Binary Numbers Quickly

For small binary numbers, you can often convert using a mental table. Up to 4 bits:

BinaryDecimal
00000
00011
00102
00113
01004
01015
01106
01117
10008
10019
101010
101111
110012
110113
111014
111115

In networking, 8 bits (one byte) is a very common size. The place values for 8 bits are especially important.

Bit position (from right)PowerValue
0$2^0$1
1$2^1$2
2$2^2$4
3$2^3$8
4$2^4$16
5$2^5$32
6$2^6$64
7$2^7$128

You will see these values again in IP addressing.

Converting Decimal to Binary: Repeated Division Method

To convert decimal to binary, a standard method uses repeated division by 2 and keeps track of the remainders.

Algorithm

  1. Divide the decimal number by 2.
  2. Record the remainder (0 or 1).
  3. Take the quotient and divide it again by 2.
  4. Repeat until the quotient is 0.
  5. The binary number is the sequence of remainders read from last to first.

Example 1: Convert $13_{10}$ to binary

Perform divisions:

StepOperationQuotientRemainder
1$13 \div 2$61
2$6 \div 2$30
3$3 \div 2$11
4$1 \div 2$01

Now read remainders from bottom to top: 1101

So $13_{10} = 1101_2$.

Check by converting back: $8 + 4 + 0 + 1 = 13$.

Example 2: Convert $50_{10}$ to binary

Divisions:

StepOperationQuotientRemainder
1$50 \div 2$250
2$25 \div 2$121
3$12 \div 2$60
4$6 \div 2$30
5$3 \div 2$11
6$1 \div 2$01

Read from bottom to top: 110010

So $50_{10} = 110010_2$, which matches the earlier example.

Rule for decimal to binary (division method):
Repeatedly divide by 2, record each remainder, and read the remainders from last to first to get the binary number.

Converting Decimal to Binary: Subtraction (Place Value) Method

Another way uses the place values directly. This method is very useful for 8 bit chunks, which appear constantly in IP addresses.

Algorithm

  1. List powers of 2 from largest down to 1 that are less than or equal to your number.
  2. Starting from the largest value, ask: can I subtract this value without going below 0?
  3. If yes, write 1 for that bit and subtract this value from the current number.
  4. If no, write 0 for that bit, and do not subtract.
  5. Move to the next lower power of 2 and repeat until you reach 1.
  6. The sequence of 1s and 0s is the binary representation.

Example 1: Convert $13_{10}$ with subtraction method

List powers of 2 less than or equal to 13:

128, 64, 32, 16 are too large, the largest that fits is 8, so use:
8, 4, 2, 1

Current number: 13

Check each value:

ValueCan we subtract?BitNew current number
813 ≥ 8 yes113 − 8 = 5
45 ≥ 4 yes15 − 4 = 1
21 ≥ 2 no01
11 ≥ 1 yes11 − 1 = 0

Bits: 1 1 0 1, so $13_{10} = 1101_2$.

Example 2: Convert $156_{10}$ to 8 bit binary

For an 8 bit value, the powers of 2 are fixed:

Bit positionValue
7128
664
532
416
38
24
12
01

Current number: 156

Check each value:

ValueCan we subtract?BitNew current number
128156 ≥ 128 yes1156 − 128 = 28
6428 ≥ 64 no028
3228 ≥ 32 no028
1628 ≥ 16 yes128 − 16 = 12
812 ≥ 8 yes112 − 8 = 4
44 ≥ 4 yes14 − 4 = 0
20 ≥ 2 no00
10 ≥ 1 no00

Bits: 1 0 0 1 1 1 0 0

So $156_{10} = 10011100_2$.

This subtraction method is the same idea as how you convert binary to decimal, but in reverse.

Tip: For 8 bit values in networking, always think in the fixed set of place values:
128, 64, 32, 16, 8, 4, 2, 1.

Leading Zeros and Fixed Bit Lengths

In pure mathematics, binary numbers do not need leading zeros. For example, 101 and 00000101 represent the same value.

In networking, it is often necessary to use a fixed number of bits, usually 8, 16, 32, or 128. For example, a typical IP address is written in four decimal numbers, but each of those is actually 8 bits internally.

If your conversion gives fewer bits than required, you add leading zeros on the left.

Example: Write 13 as an 8 bit binary value

From earlier, $13_{10} = 1101_2$.

As an 8 bit value, add zeros to the left:

1101 → 00001101

So the 8 bit representation is 00001101.

Example: Write 5 as an 8 bit binary value

$5_{10} = 101_2$.

As 8 bits, write 00000101.

Rule:
Leading zeros on the left do not change the numeric value of a binary number, but are important when a fixed number of bits is required.

Common 8 Bit Values in Networking

For networking tasks, certain decimal values and their 8 bit binary forms appear very often. Learning these helps a lot later.

DecimalBinary (8 bit)
000000000
100000001
200000010
400000100
800001000
1600010000
3200100000
6401000000
12810000000
25511111111

255 is especially important because in 8 bits it is the largest value: all bits set to 1.

You will see 0, 255, and many values between them very often when working with IP addresses and subnet masks.

Checking Your Conversions

It is easy to make small mistakes when converting. One of the best habits is to check your work in the opposite direction.

  1. If you converted decimal to binary, immediately convert the binary back to decimal using place values.
  2. See if you get the original decimal number.
  3. If not, find where a bit was set incorrectly.

Example: Check $156_{10} = 10011100_2$

Take 10011100 and apply place values:

BitValueContribution
1128128
0640
0320
11616
188
144
020
010

Add: $128 + 16 + 8 + 4 = 156$, so the conversion is correct.

Good practice:
Whenever possible, verify conversions by converting back in the other direction. This catches many small errors early.

Summary

Decimal and binary are two ways to write the same numeric values. Networking devices use binary internally, while humans usually see decimal. Converting between these forms relies on the concept of place values and powers of 2.

To convert binary to decimal, expand each bit by its power of 2 and add the values where the bit is 1. To convert decimal to binary, you can use repeated division by 2 with remainders, or the subtraction method using powers of 2.

In networking contexts you will often work with fixed bit lengths, especially 8 bits. Leading zeros are used to fill out these fixed sizes. The skills learned here will be used repeatedly in later chapters, especially when you work with IP addressing and subnetting.

Views: 46

Comments

Please login to add a comment.

Don't have an account? Register now!