Chapter 1 Number Bases, Conversion and Operations
Reading Materials: Croft, A. and R. Davidson Foundation maths. (Harlow: Pearson, 2016) 6th edition. Chapter 14 Number Bases
1.1 Number Bases
Decimal System
The numbers that we commonly used are based on 10. For example: \[\begin{equation} \label{eq1} \begin{split} 253 & = 200 + 50 + 3 \\ & = 2(100) + 5(10) + 3 \\ & = 2(10^2)+ 5(10^1) + 3(10^0) \end{split} \end{equation}\]
Binary System
A binary system uses base 2, it only consist of 2 digits, 0 and 1. Numbers in base 2 are called binary digits or simply bits. Consider the binary number \(110101_2\). As the base is 2, this means that power of 2 essentially replace powers of 10. Let us convert it to base 10. \[\begin{equation} \label{eq2} \begin{split} 110101_2 & = 1(2^5)+1(2^4)+0(2^3)+1(2^2)+0(2^1)+1(2^0) \\ & = 1(32)+1(16)+0(8)+1(4)+0(2)+1(1) \\ & = 32+16+4+1 \\ & = 53_{10} \end{split} \end{equation}\]
Octal System
Octal numbers use 8 as a base. The eight digits used in the octal system are 0, 1, 2, 3, 4, 5, 6 and 7. Octal numbers use powers of 8, just as decimal numbers use powers of 10 and binary numbers use powers of 2. Example: \[\begin{equation} \label{eq3} \begin{split} 325_8 & = 3(8^2)+2(8^1)+5(8^0) \\ & = 3(64)+2(8)+5(1) \\ & = 192+16+5 \\ & = 213_{10} \end{split} \end{equation}\]
Hexadecimal System
Hexadecimal system use 16 as a base. The digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. Example: \[\begin{equation} \label{eq4} \begin{split} 93\text{A}_{16} & = 9(16^2)+3(16^1)+\text{A}(16^0) \\ & = 9(256)+3(16)+10(1) \\ & = 2304+48+10 \\ & = 2362_{10} \end{split} \end{equation}\]
General Formula for Numbers in Any Base
A number in any base \(b\) can be represented using a general formula that is essentially a weighted sum of its digits, where each weight is a power of the base. For a number \(N\) with integer and fractional parts, expressed in base \(b\) as \(d_n d_{n-1} \dots d_1 d_0 . d_{-1} d_{-2} \dots d_{-m}\), the value of the number can be calculated using the following formula:
\[\begin{equation} N = \sum_{i=0}^{n} d_i b^i + \sum_{j=1}^{m} d_{-j} b^{-j} \end{equation}\]
Let’s break down this formula:
- \(N\): This is the value of the number in base 10 (decimal).
- \(b\): This is the base of the number system (e.g., 2 for binary, 8 for octal, 10 for decimal, 16 for hexadecimal).
- \(d_i\): This represents the digit at position \(i\) to the left of the decimal point. The positions are indexed starting from 0, so \(d_0\) is the digit right before the decimal point, \(d_1\) is the digit to its left, and so on.
- \(d_{-j}\): This represents the digit at position \(j\) to the right of the decimal point. The positions are indexed starting from 1, so \(d_{-1}\) is the first digit after the decimal point, \(d_{-2}\) is the second, and so on.
- \(b^i\): This is the weight for the integer part of the number. Each digit \(d_i\) is multiplied by the base raised to the power of its position.
- \(b^{-j}\): This is the weight for the fractional part of the number. Each digit \(d_{-j}\) is multiplied by the base raised to a negative power of its position.
- \(\sum_{i=0}^{n} d_i b^i\): This is the sum of the products of each digit in the integer part and its corresponding power of the base.
- \(\sum_{j=1}^{m} d_{-j} b^{-j}\): This is the sum of the products of each digit in the fractional part and its corresponding negative power of the base.
1.2 Why different bases matter? (optional)
Not required for this course, but useful to know.
At its core, the reason we use different number bases is efficiency and human readability. Computers fundamentally operate using binary (base-2) because their hardware (transistors, logic gates) can most easily represent two states: on (1) and off (0). However, writing out and interpreting long strings of 1s and 0s is extremely tedious and error-prone for humans.
This is where octal (base-8) and hexadecimal (base-16) become essential. They serve as a shorthand for binary, allowing us to represent large binary numbers in a much more compact, readable, and manageable form.
1.2.1 Comparison of Typical Use Cases
Number Base | Typical Use Cases | Key Advantage | Example (Value = 255 in Decimal) |
---|---|---|---|
Binary (Base-2) | • CPU instruction sets • Memory addressing • Digital logic circuits • Boolean algebra / Bitmasking |
The native language of hardware. Directly represents the on/off state of electrical circuits. | 11111111 |
Octal (Base-8) | • Historical systems (e.g., PDP-8, Unix file permissions) • Occasionally in aviation (e.g., transponder codes) |
Provides a more compact shorthand than binary for systems where the word size is divisible by 3. | 377 |
Hexadecimal (Base-16) | • Memory addresses • Machine code / Assembly language • Representing raw data (e.g., color codes #RRGGBB, network MAC addresses) • Debugging and reverse engineering |
The primary shorthand for binary. One digit represents a 4-bit nibble, making it extremely compact and easy to convert to/from binary. | FF |
In summary: Binary is for machines, decimal is for people, and hex/octal are the crucial bridges that allow developers and engineers to work with machine-level data efficiently. Hexadecimal has become the undisputed standard for this role in modern computing.
1.2.2 Why They’re Important
Binary is essential because it’s the only language that computers understand. However, a single byte (8 bits) of binary data, like 01011010
, can be hard for a person to read, write, and remember. That’s where octal and hexadecimal come in as shorthand notations. They group binary digits into larger chunks, making long binary strings more manageable.
For example, the 8-bit binary number 11111010
is represented as 372
in octal and FA
in hexadecimal. Both octal and hexadecimal are significantly shorter and less prone to human error than their binary equivalent. Hexadecimal is now the preferred standard because a single hexadecimal digit corresponds to exactly four binary digits (a nibble), which aligns perfectly with the byte (8 bits) and word (16, 32, or 64 bits) architectures common in modern computing. This makes it ideal for tasks like memory addressing, color representation in web development (e.g., #FF5733
), and debugging.
1.3 Number Conversion
1.3.1 Converting from Decimal to Other Number Base
The no-brainer way is to divide the number by the base, the remainder would be the last digit of the new number base. Keep dividing the quotient until it is smaller than the number base. Let us convert \(253_{10}\) as example. \[\begin{equation} \label{eq5} \begin{split} 2{\overline{\smash{\big)}\,253\phantom{)}}} & = 126 \text{ with remainder }1\\ 2{\overline{\smash{\big)}\,126\phantom{)}}} & = 63 \text{ with remainder }0\\ 2{\overline{\smash{\big)}\,63\phantom{)}}} & = 31 \text{ with remainder }1\\ 2{\overline{\smash{\big)}\,31\phantom{)}}} & = 15 \text{ with remainder }1\\ 2{\overline{\smash{\big)}\,15\phantom{)}}} & = 7 \text{ with remainder }1\\ 2{\overline{\smash{\big)}\,7\phantom{)}}} & = 3 \text{ with remainder }1\\ 2{\overline{\smash{\big)}\,3\phantom{)}}} & = 1 \text{ with remainder }1\\ 2{\overline{\smash{\big)}\,1\phantom{)}}} & = 0 \text{ with remainder }1 \end{split} \end{equation}\] Thus, \(253_{10}\) is \(11111101_2\) in binary. We could do the same to other number bases. Another method is by listing the powers of the base, compare and subtract. Using the same number as example.
\(2^0 = 1\) | \(2^1 = 2\) | \(2^2 = 4\) |
---|---|---|
\(2^3 = 8\) | \(2^4 = 16\) | \(2^5 = 32\) |
\(2^6 = 64\) | \(2^7 = 128\) | \(2^8 = 256\) |
From here we compare the number that we are going to convert with the list: \[\begin{equation} \label{eq6} \begin{split} 253 - 1(128) & = 125 \\ 125 - 1(64) & = 61 \\ 61 - 1(32) & = 29 \\ 29 - 1(16) & = 13 \\ 13 - 1(8) & = 5 \\ 5 - 1(4) & = 1 \\ 1 - 0(2) & = 1\\ 1- 1(1) & = 0 \end{split} \end{equation}\] Thus, \(253_{10}\) is \(11111101_2\) in binary. Like the other method, we can also do this to convert to other number bases.
1.3.2 Conversion Shortcuts with Binary Number
Converting binary numbers to Octal or Hexadecimal and vice versa are very straightforward. It can be performed without converting to Decimal first. Let us use number \(11100110_2\) as example: \[\begin{equation} \label{eq7} \begin{split} \underbrace{11}_\text{3}\underbrace{100}_\text{4}\underbrace{110}_\text{6} & = 346_8\\ \underbrace{1110}_\text{E}\underbrace{0110}_\text{6} & = \text{E}6_{16} \end{split} \end{equation}\]
1.3.3 Non-integer Number Conversion
Converting non-integer number might look counter-intuitive and intimidating. It is actually rather simple. Let us convert \(17.375_{10}\) to binary. \[\begin{split} 17.375 & = 10 + 7 + 0.3+0.07+0.005 \\ & = 1(10^1) + 7(10^0) + 3(10^{-1}) + 7(10^{-2})+5(10^{-3}) \end{split}\] Converting to binary, \(17_{10} = 10001_2\). But how about the decimal point? We multiply them by two until we are left with whole number \[\begin{equation} \label{eq8} \begin{split} 0.375 \times 2 = 0.75 = 0+0.75 & \text{ we have 0 at power }-1\\ 0.75 \times 2 = 1.5 = 1+0.5 & \text{ we have 1 at power }-2\\ 0.5 \times 2 = 1.0 = 1 & \text{ we have 1 at power }-3 \end{split} \end{equation}\] Thus, \(17.375_{10} = 10001.011_2\) The reverse is much simpler. \[\begin{equation} \label{eq9} \begin{split} 1101.101_2 & = 1(2^3)+1(2^2)+0(2^1)+1(2^0)+1(2^{-1})+0(2^{-2})+1(2^{-3}) \\ & = 1(8)+1(4)+0(2)+1(1)+1(0.5)+0(0.25)+1(0.125) \\ & = 8 + 4 + 1+ 0.5+0.125 \\ & = 13.625_{10} \end{split} \end{equation}\]
1.4 Operations with Binary Number
Addition
Addition is rather straightforward, just like with decimal numbers. Here we use \(11001001_2\) and \(11111111_2\) as example. In decimal they are 201 and 255 respectively. \[\begin{equation}\label{eq10} \begin{array}{B3} \overset{1}0 & \overset{1}1\overset{1}1\overset{1}0\overset{1}0 & \overset{1}1\overset{1}0\overset{1}01 \\ {} + 0 & 1111 & 1111\\ \hline 1 & 1100 & 1000\\ \end{array} \end{equation}\] \[\begin{equation} \label{eq11} \underbrace{11001001}_\text{201}+\underbrace{11111111}_\text{255} = \underbrace{1 1100 1000}_\text{456} \end{equation}\]
Subtraction
Likewise for subtraction, between \(111 0011_2\) and \(101 0010_2\) \[\begin{equation}\label{eq12} \begin{array}{B3} 1 1 1 & 0 0 11\\ {} - 101 & 0010\\ \hline 010 & 0001 \\ \end{array} \end{equation}\] \[\begin{equation} \label{eq13} \underbrace{1110011}_\text{115}-\underbrace{1010010}_\text{82} = \underbrace{100001}_\text{33} \end{equation}\]
Multiplication
Multiplication is essentially addition done multiple times. Let us have multiplication of \(1100_2\) and \(1111_2\). I would skip the carry so it wouldn’t look too cramped. \[\begin{equation}\label{eq14} \begin{array}{c} \phantom{\times9999}1100\\ \underline{\times\phantom{9999}1111}\\ \phantom{\times9999}1100\\ \phantom{\times999}1100\phantom9\\ \phantom{\times99}1100\phantom{99}\\ \underline{\phantom{\times9}1100\phantom{999}}\\ \phantom\times10110100 \end{array} \end{equation}\] \[\begin{equation} \label{eq15} \underbrace{1100}_\text{12}\times\underbrace{1111}_\text{15} = \underbrace{10110100}_\text{180} \end{equation}\]
Division
Division is perhaps the one that feels the most unnatural and most likely to cause mistakes. Let us do this with \(11100110_2\) divided with \(110_2\) as example. \[\begin{equation}\label{eq16} \begin{array}{r} 100110\phantom{)} \\ 110{\overline{\smash{\big)}\,11100110\phantom{)}}}\\ \underline{-~\phantom{(}110\phantom{99999)}}\\ \phantom{\times{99}}10\phantom{9999)}\\ \underline{-~\phantom{()}0\phantom{9999)}}\\ \phantom{\times{99}}100\phantom{999)}\\ \underline{-~\phantom{()}0\phantom{999)}}\\ \phantom{\times{99}}1001\phantom{99)}\\ \underline{-~\phantom{()}110\phantom{99)}}\\ \phantom{\times{999}}111\phantom{9)}\\ \underline{-~\phantom{()}110\phantom{9)}}\\ \phantom{\times{9999}}10\phantom{)}\\ \underline{-~\phantom{()}0\phantom{)}}\\ 10\phantom{)} \end{array} \end{equation}\]
\[\begin{equation} \label{eq17} \underbrace{11100110}_\text{230}\div\underbrace{110}_\text{12} = \underbrace{100110}_\text{38} \text{ with remainder }\underbrace{10}_\text{2} \end{equation}\]
1.5 Negative Numbers in Binary (optional)
Not required for this course, but useful to know.
So far, we have only worked with unsigned binary numbers, which can represent non-negative integers.
To handle negative values, computers use special representations. The three most common are:
Sign-and-Magnitude
The most significant bit (MSB) is the sign bit:
0
for positive,1
for negative.Example (with 4 bits):
0101₂ = +5
1101₂ = -5
Drawback: arithmetic becomes complicated (two versions of zero).
One’s Complement
Negative numbers are formed by inverting all bits of the positive number.
Example (4 bits):
0101₂ = +5
1010₂ = -5
(one’s complement of 0101)
Still has two versions of zero:
0000
and1111
.
Two’s Complement
Negative numbers are formed by inverting all bits and adding 1.
Example (4 bits):
0101₂ = +5
- Invert →
1010₂
- Add 1 →
1011₂ = -5
Only one zero, and arithmetic works cleanly with addition/subtraction.
Two’s complement is the most widely used method for representing negative numbers in computer systems. It overcomes the limitations of the previous two methods by providing a single representation for zero and simplifying arithmetic operations, allowing subtraction to be performed as addition.