Number Systems
Computers and digital systems work with several number systems. Each is defined by its base (or radix) — the number of unique digits it uses.
| System | Base | Digits Used |
|---|---|---|
| Binary | 2 | 0, 1 |
| Octal | 8 | 0–7 |
| Decimal | 10 | 0–9 |
| Hexadecimal | 16 | 0–9, A–F (where A=10, B=11, C=12, D=13, E=14, F=15) |
In any positional number system, the value of a digit depends on both the digit itself and its position. The rightmost position carries weight base⁰, the next carries base¹, and so on moving left.
Number System Conversions
Any Base → Decimal
Multiply each digit by its positional weight (base raised to that position's power) and sum the results.
Binary to Decimal:
1001₂ = 1×2³ + 0×2² + 0×2¹ + 1×2⁰ = 8 + 0 + 0 + 1 = 9₁₀
11000₂ = 1×2⁴ + 1×2³ + 0×2² + 0×2¹ + 0×2⁰ = 16 + 8 = 24₁₀
Octal to Decimal:
345₈ = 3×8² + 4×8¹ + 5×8⁰ = 192 + 32 + 5 = 229₁₀
Hexadecimal to Decimal:
A3₁₆ = 10×16¹ + 3×16⁰ = 160 + 3 = 163₁₀
Decimal → Any Base
Repeatedly divide the decimal number by the target base. The remainders, read from last to first, give the result.
Decimal to Binary:
36 ÷ 2 = 18 r 0 (LSB)
18 ÷ 2 = 9 r 0
9 ÷ 2 = 4 r 1
4 ÷ 2 = 2 r 0
2 ÷ 2 = 1 r 0
1 ÷ 2 = 0 r 1 (MSB)
Result: 100100₂
Decimal to Octal:
229 ÷ 8 = 28 r 5 (LSB)
28 ÷ 8 = 3 r 4
3 (MSB)
Result: 345₈
Decimal to Hexadecimal:
759 ÷ 16 = 47 r 7 (LSB)
47 ÷ 16 = 2 r 15 = F
2 (MSB)
Result: 2F7₁₆
Binary ↔ Octal
Group binary digits in sets of 3 (from the right, padding with leading zeros if needed). Each group maps to one octal digit.
100 010 111₂ → 4 2 7₈
Binary ↔ Hexadecimal
Group binary digits in sets of 4 (from the right). Each group maps to one hex digit.
1001 1110 0111 0000₂ → 9 E 7 0₁₆
Hexadecimal → Octal
Convert hex → binary first, then binary → octal.
Fractional Number Conversion
For numbers with a fractional part, handle the integer and fractional portions separately.
Fractional Binary to Decimal: Digits to the right of the binary point carry negative powers of 2: 2⁻¹ = 0.5, 2⁻² = 0.25, 2⁻³ = 0.125, etc.
1110.011₂ = 1×2³ + 1×2² + 1×2¹ + 0×2⁰ + 0×2⁻¹ + 1×2⁻² + 1×2⁻³
= 8 + 4 + 2 + 0 + 0 + 0.25 + 0.125
= 14.375₁₀
Decimal Fraction → Binary: Repeatedly multiply the fractional part by 2. The digit before the decimal point in each result (0 or 1) becomes the next binary digit after the point.
0.2 × 2 = 0.4 → 0
0.4 × 2 = 0.8 → 0
0.8 × 2 = 1.6 → 1
0.6 × 2 = 1.2 → 1
(pattern repeats: 0011...)
So 0.2₁₀ ≈ 0.001100110011...₂
Number System Arithmetic
Addition
Addition in any base works the same way as decimal — add column by column from right to left, carrying when the sum reaches or exceeds the base.
Binary carries when the sum reaches 2:
1011
+ 0110
------
10001
Hexadecimal carries when the sum reaches 16:
ACE
+ BEA
-----
16B8 (A+B=21=15+carry, etc.)
Subtraction: Direct Method
Borrow from the next higher position when the digit being subtracted is larger than the digit above it — exactly as in decimal subtraction.
Binary Subtraction Using Complements
1's Complement: Flip every bit (0→1, 1→0).
1's complement of 10110₂ → 01001₂
2's Complement: Take the 1's complement and add 1.
1's complement of 10110₂ = 01001₂
01001₂ + 1 = 01010₂
Subtraction via 1's Complement:
- Find the 1's complement of the number being subtracted.
- Add it to the other number.
- If a carry results, remove it and add 1 to the result.
- If no carry results, the answer is negative: take the 1's complement of the result and add a minus sign.
Subtraction via 2's Complement:
- Find the 2's complement of the number being subtracted.
- Add it to the other number.
- If a carry results, discard it — the remaining bits are the answer.
- If no carry, the result is in 2's complement form (negative).
The reason computers use 2's complement for negative numbers: subtraction can be performed entirely using addition circuits, eliminating the need for dedicated subtraction hardware and simplifying the CPU design.
Multiplication and Division
Binary multiplication is repeated addition of partial products — simpler than decimal since each partial product is either 0 or the multiplicand itself. Binary division follows the same long-division procedure as decimal division.
Data Representation
Everything a computer stores and processes is ultimately represented as binary sequences. The same bit pattern can mean different things depending on which encoding is being used — context and the agreed-upon code are what give the bits their meaning.
Decimal Digit Representation
Binary Coded Decimal (BCD) Each decimal digit is encoded separately using exactly 4 bits. This avoids conversion errors when working with decimal numbers.
| Decimal | BCD |
|---|---|
| 0 | 0000 |
| 1 | 0001 |
| ... | ... |
| 9 | 1001 |
Example: 789 in BCD = 0111 1000 1001
Unpacked Decimal Format (UDF / Zoned Decimal)
Each decimal digit occupies one full byte. The high 4 bits are always 1111 (the "zone" bits) except in the least significant digit, where the zone bits represent the sign (1100 = positive/zero, 1101 = negative).
Example: 789 → 11110111 11111000 11001001
Packed Decimal Format (PDF) Two decimal digits are packed into one byte, saving space. The least significant 4 bits of the entire number hold the sign code.
Example: 789 → 0111 1000 1001 1100
Signed Binary Number Representation
Sign-Magnitude
An extra bit is used as the sign: 0 for positive, 1 for negative. The remaining bits hold the magnitude.
+44₁₀ = 0101100₂ (sign bit 0, magnitude 101100)
-7₁₀ = 1000111₂ (sign bit 1, magnitude 0000111)
Absolute Value Representation
An 8-bit scheme where the leftmost bit is the sign and the remaining 7 bits are the number's value. Limitation: zero has two representations (00000000 and 10000000), which complicates arithmetic.
Complement Representation
Covered in the arithmetic section above. 2's complement is the standard approach used in modern computers because it allows subtraction to be handled by addition circuits.
Floating Point Representation
Real numbers (numbers with fractional parts) are stored in a format similar to scientific notation, standardized by IEEE.
Single precision (32-bit) layout:
| S | EEEEEEEE | FFFFFFFFFFFFFFFFFFFFFFF |
1 bit 8 bits 23 bits
- S (Sign): 0 = positive, 1 = negative
- E (Exponent): Stored as a biased value. True exponent = E − 127
- F (Mantissa/Fraction): The digits after the binary point in normalized form. A leading 1 is always implied (the "hidden bit"), giving 24 bits of precision from 23 stored bits.
True value = (−1)ˢ × 1.F × 2^(E−127)
Example — converting 64.2₁₀ to single precision:
- Convert: 64 = 1000000₂; 0.2 ≈ 0.001100110011...₂ → combined: 1000000.00110011...₂
- Normalize: 1.00000000110011... × 2⁶
- Biased exponent: 6 + 127 = 133 →
10000101in binary - Mantissa (23 bits after the point):
00000000110011001100110 - Final:
0 10000101 00000000110011001100110
Overflow occurs when a computed value is too large to fit in the available bits. For example, in an unsigned 3-bit system, the value 9 requires 4 bits and cannot be represented — any arithmetic that produces it has overflowed.
Character Representation
Characters are stored using agreed-upon binary codes. Two major standards:
ASCII (American Standard Code for Information Interchange)
Uses 7-bit patterns (extended to 8 bits) to represent uppercase and lowercase letters, digits 0–9, punctuation, and control characters. Key ranges:
- Uppercase A–Z: 65–90 (decimal)
- Lowercase a–z: 97–122
- Digits 0–9: 48–57
- Space: 32
Important: the character '8' (ASCII 56) is completely different from the integer 8. Uppercase and lowercase letters are also distinct ('A' ≠ 'a').
EBCDIC (Extended Binary Coded Decimal Interchange Code)
An 8-bit code developed by IBM for their System/360 line. Uses a zone-and-digit structure where the high 4 bits indicate the character group and the low 4 bits identify the specific character.
Parity Bits
During data transmission, noise can accidentally flip bits from 0 to 1 or vice versa. A parity bit is an extra bit added to a group of bits so that the total count of 1s is either always even (even parity) or always odd (odd parity). The receiver checks the parity and flags an error if it doesn't match.
Even parity rule: Count the 1s in the data bits.
- If odd, set parity bit = 1 (making the total even)
- If even, set parity bit = 0
Odd parity rule: Count the 1s in the data bits.
- If even, set parity bit = 1 (making the total odd)
- If odd, set parity bit = 0
Parity detection can catch single-bit errors but cannot correct them, and it cannot reliably detect even numbers of flipped bits.