Number Systems (Copy)
Binary Representation and Data Processing
- Why computers use binary
- Computers are built on electronic circuits that operate with two distinct voltage levels:
- High voltage → Represented as
1 - Low voltage → Represented as
0
- High voltage → Represented as
- All forms of data (numbers, text, images, audio, video) are stored and processed as a sequence of binary digits (bits).
- Logic gates (AND, OR, NOT, etc.) process binary data inside the Central Processing Unit (CPU).
- Data is stored in registers, which are small storage locations inside the CPU.
- Computers are built on electronic circuits that operate with two distinct voltage levels:
- Data conversion to binary
- Any form of data (e.g., a letter ‘A’ in text) is converted to binary before processing:
- Example: ASCII code for ‘A’ = 65 in denary → 01000001 in binary.
- Storing in binary makes it easier for a computer to perform calculations and apply logical operations.
- Any form of data (e.g., a letter ‘A’ in text) is converted to binary before processing:
Number Systems Overview
| Number System | Base | Digits Used | Example Values | Common Uses |
|---|---|---|---|---|
| Denary | 10 | 0–9 | 125, 402, 9 | Everyday counting, currency |
| Binary | 2 | 0, 1 | 1011₂, 110010₂ | Internal data storage and processing |
| Hexadecimal | 16 | 0–9, A–F | 3A₁₆, F4₁₆ | Memory addresses, colour codes, compact binary representation |
Converting Between Number Systems
1. Denary ↔ Binary
- Denary to binary (Repeated division by 2 method):
- Divide the denary number by 2.
- Record the remainder (0 or 1).
- Continue dividing the quotient by 2 until you reach 0.
- The binary number is the remainders read from bottom to top.
Example: Convert 25₁₀ to binary
25 ÷ 2 = 12 remainder 1 12 ÷ 2 = 6 remainder 0 6 ÷ 2 = 3 remainder 0 3 ÷ 2 = 1 remainder 1 1 ÷ 2 = 0 remainder 1 Binary = 11001₂ - Binary to denary (Positional value method):
- Multiply each bit by 2ⁿ, where n is the position from right (starting at 0), then sum.
Example: Convert 101101₂ to denary:
(1 × 2⁵) + (0 × 2⁴) + (1 × 2³) + (1 × 2²) + (0 × 2¹) + (1 × 2⁰) = 32 + 0 + 8 + 4 + 0 + 1 = 45₁₀
2. Denary ↔ Hexadecimal
- Denary to hexadecimal (Repeated division by 16):
Example: Convert 255₁₀ to hexadecimal:255 ÷ 16 = 15 remainder 15 15 in hex = F Read from bottom to top: FF₁₆ - Hexadecimal to denary (Positional value method):
Example: Convert 2F₁₆ to denary:(2 × 16¹) + (15 × 16⁰) = 32 + 15 = 47₁₀
3. Binary ↔ Hexadecimal
- Binary to hexadecimal:
- Group binary digits into 4-bit groups from right to left.
- Convert each group to its hexadecimal equivalent.
Example: 10101111₂ → Group as 1010 1111
1010 = A 1111 = F Result: AF₁₆ - Hexadecimal to binary:
- Convert each hexadecimal digit into a 4-bit binary value.
Example: 3C₁₆ →
3 = 0011 C = 1100 Result: 00111100₂
Why Hexadecimal is Beneficial
- Shorter representation than binary (e.g., 11111111₂ is FF₁₆).
- Easier for humans to read and write.
- Common uses in computing:
- Memory addresses (e.g., 0x3F2A)
- HTML colour codes (e.g., #FF5733)
- Machine code representation for debugging.
- Reduces human error when working with long binary strings.
Binary Addition and Overflow
Binary addition rules:
| Binary Addition | Result | Carry |
|---|---|---|
| 0 + 0 | 0 | 0 |
| 0 + 1 | 1 | 0 |
| 1 + 0 | 1 | 0 |
| 1 + 1 | 0 | 1 |
Example: Add 11001010₂ and 01110111₂
11001010
+ 01110111
-------------
100000001
- The result is 9 bits in length → overflow has occurred.
Overflow concept:
- Happens when the result of a calculation is too large for the allocated number of bits.
- 8-bit register: Maximum value = 255₁₀ (11111111₂).
- If adding results in a number > 255, an overflow occurs.
- Overflow can cause incorrect calculations or wrap-around errors in computing.
Logical Binary Shifts
Logical left shift:
- Each bit moves one position to the left.
- A
0is added to the rightmost bit. - Leftmost bit is lost.
- Effect: Multiplies the number by 2 for each shift.
Example: 00101100₂ (44₁₀) left shift by 1 → 01011000₂ (88₁₀).
Logical right shift:
- Each bit moves one position to the right.
- A
0is added to the leftmost bit. - Rightmost bit is lost.
- Effect: Divides the number by 2 for each shift.
Example: 00101100₂ (44₁₀) right shift by 1 → 00010110₂ (22₁₀).
Two’s Complement Representation
Why use two’s complement:
- Allows binary to represent both positive and negative integers.
- Simplifies binary arithmetic (same addition/subtraction rules for all integers).
8-bit two’s complement range:
- Minimum value: −128 (10000000₂)
- Maximum value: +127 (01111111₂)
Converting positive denary to two’s complement binary:
- Convert denary to binary normally (7 bits for magnitude, plus sign bit 0).
- Example: +18 → 00010010₂.
Converting negative denary to two’s complement binary:
- Convert absolute value to binary.
- Invert all bits (1 → 0, 0 → 1).
- Add 1 to the result.
Example: −18 in 8-bit:
+18 = 00010010
Invert = 11101101
Add 1 = 11101110
Final: 11101110₂.
Converting from two’s complement binary to denary:
- If MSB (most significant bit) is 0 → positive number → convert normally.
- If MSB is 1 → negative number:
- Invert all bits.
- Add 1.
- Convert to denary → add minus sign.
Example: 11101110₂:
Invert: 00010001
Add 1: 00010010 (18)
Result: −18
