BitFlow
← Academy·Number Systems
0bBeginner~30 min

Number Systems

Binary, octal and hexadecimal: reading them, converting between them, and how many bits a number needs.

Interactive·Number System Converter

Type in any field — binary, decimal, or hex sync instantly. Flip bits on the visualizer below!

Bit visualizer — click to toggle

What a digit's position means

You already use a positional number system. In 372, the 3 means three hundreds because of where it sits: 3×100 + 7×10 + 2×1. Each position is worth ten times the one to its right, because we count in base 10.

Nothing about this needs ten. In base 2, or binary, the digits are only 0 and 1, and each position is worth twice the one to its right: 1, 2, 4, 8, 16, 32 and so on. Binary suits hardware because each digit is one wire.

(d₃d₂d₁d₀)ᵣ = d₃×r³ + d₂×r² + d₁×r¹ + d₀×r⁰
r is the base, or radix. A subscript shows the base when it isn't obvious.

Loading the animation…

Worked example: Binary to decimal

What is 101101₂ in decimal?

  1. Write the position values above the digits: 32, 16, 8, 4, 2, 1.
  2. Keep the values where the digit is 1: 32 + 8 + 4 + 1.

Answer:45

Loading the circuit…

Click the number on the left to count up (shift-click counts down). The lights show the same number in binary, with b3 worth 8 and b0 worth 1.
  • Set the input to 13. Add the values of the lit bits: 8 + 4 + 1.
  • Count from 0 to 7 and watch b0. It flips every step, b1 every two steps, b2 every four.

Decimal to binary

To convert a whole number, divide by 2 repeatedly and keep the remainders. The first remainder is the rightmost bit.

StepRemainder
45 ÷ 2 = 221 (rightmost)
22 ÷ 2 = 110
11 ÷ 2 = 51
5 ÷ 2 = 21
2 ÷ 2 = 10
1 ÷ 2 = 01 (leftmost)
Reading the remainders from the bottom up gives 101101, which matches the earlier example.

Loading the animation…

A fraction works the other way around: multiply by 2 repeatedly and keep the whole-number part each time. The first one you get is the bit right after the point.

Worked example: A fraction in binary

Convert 0.625 to binary.

  1. 0.625 × 2 = 1.25, so the first bit is 1. Carry on with 0.25.
  2. 0.25 × 2 = 0.5, so the next bit is 0. Carry on with 0.5.
  3. 0.5 × 2 = 1.0, so the next bit is 1. Nothing is left, so stop.

Answer:0.101₂, which is 1/2 + 1/8.

Watch outMany simple decimals never end in binary. 0.1 becomes 0.000110011… with the 0011 repeating forever. A machine has to cut it off somewhere, which is why 0.1 + 0.2 isn't exactly 0.3 on most computers.
Practice

What is 13.25 in binary?

Hexadecimal and octal

Long strings of bits are hard to read and easy to miscopy. Hexadecimal (base 16) is a shorthand. It needs sixteen digit symbols, so it uses 0 to 9 and then A to F for ten to fifteen.

Because 16 = 2⁴, each hex digit stands for exactly 4 bits. Likewise, because 8 = 2³, each octal digit stands for exactly 3 bits. So you convert by grouping bits, starting from the point, with no arithmetic at all.

HexBinaryHexBinaryHexBinaryHexBinary
000004010081000C1100
100015010191001D1101
2001060110A1010E1110
3001170111B1011F1111
Worked example: Grouping bits

Write 1101 0110₂ in hex, and 110101₂ in octal.

  1. In fours: 1101 is D and 0110 is 6, so 1101 0110₂ = D6₁₆.
  2. As a check, D6₁₆ = 13×16 + 6 = 214.
  3. In threes: 110 is 6 and 101 is 5, so 110101₂ = 65₈.

Answer:D6₁₆ and 65₈.

TipGoing from hex to octal? Go through binary. Write out the 4-bit groups, then regroup them in threes.

In Verilog and in datasheets you'll see prefixes that name the base: 8'hD6 and 0xD6 both mean hex D6, and 4'b1010 means binary 1010.

Try the number tools →Convert any value between bases, and see it as signed and unsigned.

How big a number fits

With n bits, the largest unsigned number is all ones, which is one less than the next power of 2. So n bits hold 0 to 2ⁿ − 1.

BitsLargest valueIn hex
415F
8255FF
1010233FF
1665,535FFFF
324,294,967,295FFFFFFFF
Practice

What is the fewest bits that can hold the value 1024?

Base problems in examsGATE: fine to skip at first

Exams like questions where the base itself is unknown. Expand both sides with the positional formula and solve the equation. Remember that every digit must be smaller than the base, which often rules out one of the roots.

Worked example: Find the base

If (121)ᵣ = (144)₁₀, what is r?

  1. Expand the left side: 1×r² + 2×r + 1 = 144.
  2. That is a perfect square: (r + 1)² = 144.
  3. So r + 1 = 12 (a base can't be negative), and r = 11.
  4. All the digits 1, 2, 1 are below 11, so this base is allowed.

Answer:r = 11

Practice · GATE-style

What is (3AF)₁₆ in octal?

Practice · GATE-style

For which base r is (23)ᵣ + (14)ᵣ = (41)ᵣ?

Was this chapter helpful?