Hub 4 Mech
Note Read

Read the note, explore related problems and apps.

Underg.
Basic

Number Base Conversion: Binary, Octal, Decimal and Hex

ID: ny6n557q54

~6 min read
H4M Team
H4M TeamChief Editor

“How positional number systems work and how to convert between bases: place values, repeated division and bit grouping for binary, octal and hex.”

Views:2
Saved:0
Shares:0
Downloads:0
Comments:0

A number system writes every number with a fixed set of digits. The base (or radix) \(b\) is the number of digits it uses: ten in the decimal system, two in binary and sixteen in hexadecimal. Engineers meet other bases in digital electronics, controllers, memory addresses and colour codes, and base 60 lives on in hours, minutes and seconds and in degrees, minutes and seconds of angle.

1. Positional Notation

place values of the hexadecimal number 2F7: 2 times 16 squared, 15 times 16, 7 times 1, adding up to 759
Each digit times its place value

In a positional system the value of a digit depends on its place. The rightmost digit counts ones, the next one counts \(b\), the next \(b^2\), and so on. A number with the digits \(d_{n-1} \dots d_1 d_0\) in base \(b\) has the value

$$ N = d_{n-1}\,b^{\,n-1} + \dots + d_1\,b + d_0 = \sum_{i=0}^{n-1} d_i\,b^{\,i} $$

 

Each digit is a whole number from 0 to \(b-1\). Bases above ten need more symbols than 0–9, so letters are used: A = 10, B = 11, … F = 15 in hexadecimal, up to Z = 35 in base 36. The base is written as a subscript when the context does not make it clear: \(\mathrm{2F7}_{16}\), \(1011_{2}\), \(759_{10}\).

2. Converting a Number to Decimal

To find the decimal value, multiply each digit by its place value and add the products. For the hexadecimal number \(\mathrm{2F7}_{16}\):

$$ \mathrm{2F7}_{16} = 2 \times 16^{2} + 15 \times 16^{1} + 7 \times 16^{0} = 512 + 240 + 7 = 759_{10} $$

 

For long numbers the same sum is quicker in nested form (Horner's rule): start with the leftmost digit, then repeatedly multiply by the base and add the next digit.

$$ \mathrm{2F7}_{16} = (2 \times 16 + 15) \times 16 + 7 = 47 \times 16 + 7 = 759 $$

 

The nested form needs one multiplication per digit and no powers. It is also how a program reads a number typed in any base.

3. Converting a Decimal Number to Another Base

The nested form also shows the way back. Dividing \(N\) by \(b\) leaves the last digit \(d_0\) as the remainder; dividing the quotient again leaves \(d_1\), and so on. The method of repeated division is:

  1. Divide the number by the base and write down the whole-number quotient and the remainder.
  2. Divide the quotient by the base again; repeat until the quotient is 0.
  3. Read the remainders from the last to the first: the first remainder is the rightmost digit.
repeated division of 759 by 16 with quotients 47, 2, 0 and remainders 7, 15, 2, read upward as 2F7
Repeated division: decimal 759 to base 16

For 759 in base 16 the remainders are 7, 15 and 2, so \(759_{10} = \mathrm{2F7}_{16}\). The same steps with the base 2 give \(759_{10} = 1011110111_{2}\).

Fractions. They convert the other way round: multiply the fractional part by the base and keep the whole-number part as the next digit. \(0.625 \times 2 = 1.25\), \(0.25 \times 2 = 0.5\) and \(0.5 \times 2 = 1.0\) give \(0.625_{10} = 0.101_{2}\). Many decimal fractions, such as 0.1, never end in binary.

4. Converting Between Two Other Bases

the binary number 10110111 grouped in fours gives B7 in hexadecimal and grouped in threes gives 267 in octal
Grouping bits: 4 bits per hex digit, 3 bits per octal digit

Between two arbitrary bases, convert to decimal first and then to the target base. Bases that are powers of two have a shortcut, because \(8 = 2^{3}\) and \(16 = 2^{4}\):

  • Binary to hexadecimal: group the bits in fours from the right and replace each group by one hexadecimal digit: \(1011\,0111_{2} = \mathrm{B7}_{16}\).
  • Binary to octal: group the bits in threes: \(010\,110\,111_{2} = 267_{8}\).
  • Hexadecimal or octal to binary: write each digit as four or three bits: \(\mathrm{F}_{16} = 1111_{2}\), \(7_{8} = 111_{2}\).

Add zeros on the left to fill the leftmost group. Hexadecimal is popular because it is a short, readable form of binary: one byte (8 bits) is exactly two hexadecimal digits.

5. Number Bases in Engineering

  • Binary (base 2): digital logic, the registers of sensors and controllers, bit masks; a register value \(0110\,0100_{2} = 100\).
  • Hexadecimal (base 16): memory addresses, data sheets and colour codes such as #4F46E5 (red 4F = 79, green 46 = 70, blue E5 = 229).
  • Octal (base 8): file permissions on Linux, such as 755.
  • Base 60: time (1 h = 60 min = 3600 s) and angles (1° = 60′ = 3600″).
  • Bases 12 and 24: the hours of a clock.

Only the way a number is written changes with the base; the quantity it stands for stays the same. Whole numbers up to \(2^{53} - 1 = 9\,007\,199\,254\,740\,991\) are stored exactly in the 64-bit floating-point format used by most software.

Summary

  • In base \(b\) a number is \(\sum d_i\, b^{\,i}\) with digits from 0 to \(b-1\); letters stand for digits above 9.
  • To decimal: add the digits times their place values, or use the nested form.
  • From decimal: divide repeatedly by the base and read the remainders upward.
  • Binary to hexadecimal or octal: group the bits in fours or threes.
  • The base changes how a number is written, not its value.

Continue from here

What builds on this note

App
Unit Converter: Engineering
Tool

Unit Converter: Engineering

Convert 349 units in 25 quantities between SI, CGS, kgf, U.S. and imperial systems: force, stress, energy, flow, viscosity, number bases and more.

1
128
0
0
0
0

Contributors

Community members

Comments 0

Ask questions, explain a step or share a tip about this note.

Join the discussion

Log in to comment, reply and vote.

No comments yet

Ask a question, share a tip or say thanks. Be the first!

0

Table of Contents

Navigate through the document