CE2704 · Digital Logic Design
Theme 1 · Numbers & codes

Base conversions

The same number, written for three different readers — you (decimal), the machine (binary), and the datasheet (hex). This topic is how you move between them.

Built from first principles. Hex notation follows common datasheet convention (e.g. the MPU6050 register map).

Before you start

What you need first

  • Number bases & place value — that each digit sits in a column worth a power of the base (binary: …8 4 2 1; hex: …256 16 1).
  • The hex digits — 0–9 then A B C D E F stand for 10 11 12 13 14 15.
  • Basic arithmetic — multiply, add, and divide-with-remainder.

What you'll be able to do

  • Convert binary → decimal with the place-value method.
  • Convert decimal → binary by repeated division by 2.
  • Convert hex ↔ binary instantly, four bits at a time.
  • Convert hex ↔ decimal, and know the smart route between them.

Motivation

One number, three sets of clothes

A microcontroller stores everything as binary — rows of on/off bits. But raw binary is unreadable for a human: nobody can glance at 10110101 and know what it means.

So we wear different "clothes" for the same value. You think in decimal. A datasheet writes addresses and settings in hex because it is short. The chip works in binary. Converting between them is the everyday skill that lets all three talk.

The value never changes — only how we write it. 181, 10110101₂, and 0xB5 are the same number.
the value 181 181 (decimal) 10110101₂ 0xB5
The same value, three notations.

Binary → decimal: add the place values

Every bit sits in a column worth a power of two. To read a binary number in decimal, write each column's place value, keep only the columns where the bit is 1, and add them up.

📐 Worked example

Convert 10110101₂ to decimal

Lay the bits over their powers of two (rule of the house: show the power, not just the number):

Position76543210
Power2⁷2⁶2⁵2⁴2⁰
Place value1286432168421
Bit10110101
Adds128·3216·4·1

Add only the columns whose bit is 1:

$$ 128 + 32 + 16 + 4 + 1 = 181 $$
Quick sanity check: 8 bits hold 0 … \(2^8-1 = 255\). Our answer 181 is in range. ✓

✏️ Try it yourself

Convert 11010110₂ to decimal. (Tip: write the place values 128 64 32 16 8 4 2 1 above the bits.)

Bits set: positions 7, 6, 4, 2, 1 → place values 128, 64, 16, 4, 2. Add: \(128 + 64 + 16 + 4 + 2 = 214\). Answer: 11010110₂ = 214.

Decimal → binary: divide by 2, read up

Go the other way by repeated division by 2. Each division leaves a remainder of 0 or 1 — those remainders are the bits. Read them bottom-to-top.

📐 Worked example

Convert 181 to binary

Divide=QuotientRemainder
181 ÷ 2=901← LSB (read last)
90 ÷ 2=450
45 ÷ 2=221
22 ÷ 2=110
11 ÷ 2=51
5 ÷ 2=21
2 ÷ 2=10
1 ÷ 2=01← MSB (read first)

Read the remainders from the bottom up:

$$ 181 = 1011\,0101_2 $$
Faster, with practice: subtract the biggest power of 2 that fits and repeat — \(181-128=53,\ 53-32=21,\ 21-16=5,\ 5-4=1,\ 1-1=0\) → bits 7,5,4,2,0 set → 10110101. Same answer.

✏️ Try it yourself

Convert 200 to binary by repeated division by 2.

Divide: 200→100 r0, 100→50 r0, 50→25 r0, 25→12 r1, 12→6 r0, 6→3 r0, 3→1 r1, 1→0 r1. Read up: 1 1 0 0 1 0 0 0. Answer: 200 = 11001000₂. (Check: 128+64+8 = 200 ✓)

The easy one

Hex ↔ binary: four bits at a time

This is the conversion you will use most, and it needs no arithmetic at all. Because \(16 = 2^4\), one hex digit is exactly four binary bits (a "nibble"). Just group and substitute.

1 0 1 1 B 0 1 0 1 5 split a byte into two 4-bit nibbles
Each nibble maps to one hex digit: 1011 01010xB5.
📐 Worked example

Both directions

Binary → hex. Split into 4-bit groups from the right, then look each up:

Binary10110101
HexB5
$$ 1011\,0101_2 = \mathtt{0xB5} $$

Hex → binary. Replace each hex digit with its 4 bits:

Hex2F
Binary00101111
$$ \mathtt{0x2F} = 0010\,1111_2 $$
This is why hex exists. No human reads 32 raw bits at a glance — but anyone can read 0x2F as 0010 1111 after a few minutes of practice.

✏️ Try it yourself

(a) Convert 0x3C to binary.   (b) Convert 11100101₂ to hex.

(a) 3 → 0011, C → 1100, so 0x3C = 00111100₂. (b) group: 1110 0101 → E 5, so 11100101₂ = 0xE5.

Hex → decimal: powers of 16

Same place-value idea as binary, but the columns are now powers of 16: \(16^0=1,\ 16^1=16,\ 16^2=256,\ 16^3=4096,\dots\) Remember A–F mean 10–15.

📐 Worked example

Convert 0x2F to decimal

Position10
Power16¹16⁰
Place value161
Hex digit2F = 15
$$ 2\times 16 \;+\; 15\times 1 \;=\; 32 + 15 \;=\; 47 $$

Decimal → hex: divide by 16, read up

Exactly the repeated-division trick again — but divide by 16 instead of 2. If a remainder is 10–15, write it as A–F.

📐 Worked example

Convert 181 to hex

Divide=QuotientRemainder
181 ÷ 16=115← read last
11 ÷ 16=011 = B← read first

Read the remainders bottom-to-top:

$$ 181 = \mathtt{0xB5} $$

✏️ Try it yourself

(a) Convert 0x4A to decimal.   (b) Convert 140 to hex.

(a) \(4\times16 + 10\times1 = 64 + 10 = 74\). So 0x4A = 74. (b) 140 ÷ 16 = 8 r12 (=C); 8 ÷ 16 = 0 r8. Read up: 8 C. So 140 = 0x8C. Check (b): \(8\times16 + 12 = 128 + 12 = 140\) ✓

In practice

Don't go decimal ↔ hex directly — go through binary

The divide-by-16 method works, but almost nobody uses it. The easy path is to route through binary, because hex ↔ binary is instant and you already know binary ↔ decimal.

decimal → binary → hex,  and  hex → binary → decimal. The binary middle step turns the hard conversion into two easy ones.
decimal binary hex ÷2 / place value group of 4
Binary is the hub.

Recap — the whole topic on one screen

ConversionMethodWorked result
Binary → decimalAdd the place values where the bit is 110110101₂ = 181
Decimal → binaryDivide by 2 repeatedly, read remainders up181 = 10110101₂
Hex → binaryReplace each hex digit with 4 bits0x2F = 00101111₂
Binary → hexGroup bits in 4s from the right, look up10110101₂ = 0xB5
Hex → decimalAdd digit × power of 160x2F = 47
Decimal → hexDivide by 16 repeatedly (or via binary)181 = 0xB5
Golden habit: when in doubt, go through binary. And always sanity-check the range (8 bits → 0…255).

Next topic

Binary addition

You can now read and write numbers in every base. Next we make the machine do something with them — starting with the simplest operation a computer performs: adding two binary numbers, carry and all. That's the seed of every adder, and of the ALU.

→ Binary addition