Number Systems and Operations
From the 12 curriculum
Number Systems and Operations
TL;DR
Understanding different number systems (like binary and hexadecimal) is crucial for how computers work, as they use these systems internally to represent data. Operations within these systems, such as addition and subtraction, follow rules similar to decimal but with different base values. Mastering these concepts helps you grasp fundamental computer science principles and low-level programming.
1. The Mental Model
Think of number systems as different languages for counting. Just as you can say "one" in English or "uno" in Spanish, you can represent the quantity "ten" as '10' in decimal, 'A' in hexadecimal, or '1010' in binary. Each system just uses a different set of symbols and a different 'base' for grouping.
2. The Core Material
Computers don't understand our decimal (base-10) numbers directly. They operate using electrical signals that are either "on" or "off," which perfectly maps to binary (base-2), using only 0s and 1s. Hexadecimal (base-16) is a convenient shorthand for binary because it lets us represent large binary numbers more compactly.
### Decimal (Base-10)

Photo by Roman Friptuleac on Pexels
This is the number system you use every day. It has ten unique digits (0-9) and each digit's position represents a power of 10.
Example: $123 = (1 \times 10^2) + (2 \times 10^1) + (3 \times 10^0)$
### Binary (Base-2)

Photo by Google DeepMind on Pexels
Binary uses only two digits: 0 and 1. Each position represents a power of 2. This is the computer's native language.
Example: $101_2 = (1 \times 2^2) + (0 \times 2^1) + (1 \times 2^0) = 4 + 0 + 1 = 5_{10}$
Converting Decimal to Binary
To convert a decimal number to binary, you repeatedly divide the decimal number by 2 and record the remainders. The binary number is then read from bottom up.
Binary Addition
Binary addition works like decimal addition, but when the sum of bits is 2, you write down 0 and carry over 1.
graph TD
A["Start with Decimal Number"] --> B{"Is Number > 0?"}
B -- Yes --> C["Divide Number by 2"]
C --> D["Record Remainder (0 or 1)"]
D --> E["Update Number to Quotient"]
E --> B
B -- No --> F["Read Remainders Bottom-Up"]
F --> G["Binary Equivalent"]
### Hexadecimal (Base-16)

Photo by Bibek ghosh on Pexels
Hexadecimal uses 16 unique "digits": 0-9 and A-F (where A=10, B=11, C=12, D=13, E=14, F=15). Each position represents a power of 16. It's often used in computing to represent memory addresses, colors, and other data because each hex digit can represent exactly 4 binary bits (since $2^4 = 16$).
Example: $A5_{16} = (10 \times 16^1) + (5 \times 16^0) = 160 + 5 = 165_{10}$
Converting Binary to Hexadecimal
Group binary digits into sets of four, starting from the right. Convert each 4-bit group into its corresponding hex digit.
Example: $11010110_2$
Group: $1101 \ 0110$
Convert: $D \ 6$
Result: $D6_{16}$
Integer Overflow
This occurs when an arithmetic operation tries to create a numeric value that is larger than the available storage space (e.g., a 16-bit integer can only hold values up to $2^{16}-1$). The result "wraps around," often to a very small or negative number. This is a common bug in programming.
### Two's Complement

Photo by Markus Winkler on Pexels
Computers need a way to represent negative numbers. Two's complement is the most common method. To find the two's complement of a number:
1. Invert all bits (0s become 1s, 1s become 0s) – this is called the "one's complement."
2. Add 1 to the result.
The leftmost bit acts as the sign bit (0 for positive, 1 for negative). This system makes binary addition and subtraction straightforward for both positive and negative numbers.
3. Worked Example
Let's convert the decimal number $27_{10}$ to binary, then to hexadecimal, and finally check its 8-bit two's complement representation for $-27_{10}$.
1. Decimal to Binary ($27_{10}$):
* $27 \div 2 = 13$ remainder $1$
* $13 \div 2 = 6$ remainder $1$
* $6 \div 2 = 3$ remainder $0$
* $3 \div 2 = 1$ remainder $1$
* $1 \div 2 = 0$ remainder $1$
Reading remainders from bottom-up: $11011_2$. To make it 8-bit, we pad with leading zeros: $00011011_2$.
2. Binary to Hexadecimal ($00011011_2$):
Group into 4-bit chunks: $0001 \ 1011$.
* $0001_2 = 1_{16}$
* $1011_2 = B_{16}$ (since $B=11_{10}$)
So, $27_{10} = 1B_{16}$.
3. Two's Complement for $-27_{10}$ (using 8 bits):
* Start with positive $27_{10}$ in 8-bit binary: $00011011_2$
* Invert all bits (one's complement): $11100100_2$
* Add 1:
11100100
+ 1
----------
11100101
So, $-27_{10}$ in 8-bit two's complement is $11100101_2$.
4. Key Takeaways
- Binary (base-2) is the fundamental number system for computers, using only 0s and 1s.
- Hexadecimal (base-16) is a convenient shorthand for representing binary data, making it easier for humans to read.
- Each hexadecimal digit corresponds to exactly four binary digits, simplifying conversions between the two.
- Understanding place values (powers of the base) is key to converting between number systems.
- Two's complement is the primary method for representing negative numbers in computers, allowing for efficient arithmetic with both positive and negative values.
- Integer overflow occurs when a number exceeds the capacity of its storage, leading to unexpected results like negative wrap-arounds.
Common Mistakes to Avoid:
- Forgetting that hex digits A-F represent 10-15 when converting to decimal.
- Mixing up positive and negative signs when working with two's complement without correctly applying all steps.
- Ignoring the number of bits allocated (e.g., 8-bit, 16-bit) when performing two's complement, which affects the range and sign representation.
- Not checking for integer overflow in calculations, especially in programming contexts.
5. Now Try It
Convert the decimal value $100_{10}$ to an 8-bit binary number. Then, convert that 8-bit binary number to hexadecimal. Finally, determine the 8-bit two's complement representation for $-100_{10}$.
What success looks like: You'll have three clear number representations: a binary string, a hexadecimal string, and another binary string for the negative value, all correctly derived and showing the proper number of bits.
Frequently asked about Number Systems and Operations
Get the full 12 curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Create Free Account