Number Systems and Binary Arithmetic: How Computers Calculate

Number Systems and Binary Arithmetic: How Computers Calculate
1. Introduction
In our previous guides, we explored the logic gates that manipulate high and low voltages inside a CPU, the boot sequence that wakes a computer up, and the network channels that carry packets across the school ICT lab. Yet, at the most fundamental level, every digital computer operates on one core discipline: Number Systems.
When you type a number in a spreadsheet, compile a Python script, or save an image, the computer does not see letters, decimal values, or colors.
Instead, it converts every piece of information into electrical states represented by Binary Numbers (0s and 1s). To make handling massive sequences of binary digits manageable for human programmers and systems engineers, computer science also relies on the Octal and Hexadecimal number systems. Understanding these bases and the rules of binary arithmetic unlocks the true mathematical engine running inside every processor.
2. Visual Sketchnote: The Four Computer Number Systems
 ╔══════════════════════════════════════════════════════════════════════════════╗
 ║ NUMBER SYSTEMS QUICK REFERENCE MAP ║
 ╚══════════════════════════════════════════════════════════════════════════════╝

  [ 1. DECIMAL (Base 10) ] [ 2. BINARY (Base 2) ]
   • Human everyday counting • Computer internal hardware
   • Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 • Bits: 0, 1
   • Place values: ... 10², 10¹, 10⁰ • Place values: ... 2³, 2², 2¹, 2⁰
     Example: 25₁₀ = 2×10¹ + 5×10⁰ Example: 11001₂ = 16 + 8 + 1 = 25₁₀

 ────────────────────────────────────────────────────────────────────────────────
  [ 3. OCTAL (Base 8) ] [ 4. HEXADECIMAL (Base 16) ]
   • Shorthand for 3 binary bits • Shorthand for 4 binary bits (Nibble)
   • Digits: 0, 1, 2, 3, 4, 5, 6, 7 • Symbols: 0 - 9, and A, B, C, D, E, F
   • Group binary bits in sets of 3: (A=10, B=11, C=12, D=13, E=14, F=15)
     11001₂ ──► [011] [001] = 31₈ • Group binary bits in sets of 4:
                                                11001₂ ──► [0001] [1001] = 19₁₆

 ────────────────────────────────────────────────────────────────────────────────
  [ WHY HEXADECIMAL MATTERS IN ICT LABS ]
   • MAC Addresses : `c4-ad-34-12-fe-01` (12 Hex digits = 48 bits)
   • Web Colors : `#FF5733` (Red: FF, Green: 57, Blue: 33)
   • Memory Dumps : `0x7FFE0014` (Compact pointer addressing)
 ════════════════════════════════════════════════════════════════════════════════

3. Positional Notation & Number Bases
Any number system represents values through positional notation, where the weight of each digit depends on its position relative to the base (r):
1. Decimal (Base 10)
 * Used by humans because we have ten fingers.
 * Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
 * Example: 743_{10} = (7 \times 10^2) + (4 \times 10^1) + (3 \times 10^0) = 700 + 40 + 3.
2. Binary (Base 2)
 * Used natively by CPU circuits because transistors act as binary on/off switches.
 * Digits: 0, 1 (each digit is a bit, short for binary digit).
 * A group of 4 bits is a Nibble, and 8 bits form a Byte.
3. Hexadecimal (Base 16)
 * Because reading long strings of binary like 1101011010111100 is prone to human error, computer scientists use Hexadecimal as a compact shorthand.
 * Every hexadecimal digit represents exactly 4 binary bits (2^4 = 16).
 * Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 followed by letters A\,(10), B\,(11), C\,(12), D\,(13), E\,(14), F\,(15).
4. Converting Between Bases
Decimal to Binary (Double-Dabble / Repeated Division)
To convert a decimal integer to binary, divide repeatedly by 2 and record the remainders from bottom to top:
 2 | 29 Remainder
 ──┼───────────────
 2 | 14 - 1 ▲ (Least Significant Bit - LSB)
 2 | 7 - 0 │
 2 | 3 - 1 │
 2 | 1 - 1 │
   | 0 - 1 │ (Most Significant Bit - MSB)

 Reading upwards: 29₁₀ = 11101₂

Binary to Hexadecimal (The 4-Bit Grouping Method)
Convert binary directly to hexadecimal by splitting the bits into groups of 4 from right to left (pad with leading zeros on the left if necessary):
5. Binary Arithmetic
Just like decimal arithmetic, binary numbers can be added and subtracted directly using simple positional rules.
Rules of Binary Addition:
 * * * * * Carry: 1 1 1
           1 0 1 1₂ (Decimal: 11)
       + 0 1 1 1₂ (Decimal: 7)
       ─────────────
         1 0 0 1 0₂ (Decimal: 18)

Signed Numbers: Two's Complement
How do CPUs represent negative numbers without needing a separate minus symbol on silicon? They use Two's Complement notation:
 * Take the positive binary representation of the number.
 * Invert all bits (0 \rightarrow 1, 1 \rightarrow 0)—this is the One's Complement.
 * Add binary 1 to the result.
 Represent -5 in 8-bit Two's Complement:
  +5 in 8-bit binary: 0 0 0 0 0 1 0 1
  One's complement: 1 1 1 1 1 0 1 0
  Add 1: + 1
  ─────────────────────────────────────────
  Two's Complement (-5): 1 1 1 1 1 0 1 1

(Notice that the leftmost bit, or MSB, is 1, which immediately signifies a negative value).
6. Number Systems Comparison Matrix
| System | Base (r) | Allowable Digits | Binary Equivalence | Common Practical ICT Use |
|---|---|---|---|---|
| Binary | 2 | 0, 1 | 1 bit | Machine instructions, transistor logic, bitmasks |
| Octal | 8 | 0, 1, 2, 3, 4, 5, 6, 7 | 3 bits | Linux file system permission masks (chmod 755) |
| Decimal | 10 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | — | Human user inputs, display metrics, everyday math |
| Hex | 16 | 0-9 and A, B, C, D, E, F | 4 bits | MAC addresses, IPv6 segments, HTML/CSS hex colors |
7. Hands-on Lab Activity: Base Conversions in Python
Students can write an interactive Python script on lab computers to explore number bases and inspect how the operating system handles binary strings:
# ==========================================================
# ICT Lab Exercise: Number System Conversion Engine
# ==========================================================

def inspect_number(decimal_value):
    print(f"\n--- Analyzing Decimal: {decimal_value} ---")
    
    # Python built-in conversion representations
    binary_repr = bin(decimal_value)
    octal_repr = oct(decimal_value)
    hex_repr = hex(decimal_value)
    
    print(f"Binary Representation : {binary_repr} (Base 2)")
    print(f"Octal Representation : {octal_repr} (Base 8)")
    print(f"Hexadecimal Representation : {hex_repr.upper()} (Base 16)")
    
    # Show 8-bit padded representation
    if 0 <= decimal_value <= 255:
        padded_binary = f"{decimal_value:08b}"
        print(f"Padded 8-Bit Byte (Byte) : {padded_binary[:4]} {padded_binary[4:]}")

# Test with standard lab values
test_values = [25, 42, 255]

print("=== ICT LAB BASE CONVERSION TOOL ===")
for val in test_values:
    inspect_number(val)

# Demonstrate dynamic user testing
try:
    user_input = int(input("\nEnter any positive decimal integer to inspect: "))
    inspect_number(user_input)
except ValueError:
    print("Please enter a valid integer.")

Lab Verification Steps:
 * Open Python IDLE or the Linux terminal on your lab desktop.
 * Save the code as bases_lab.py and run it (F5).
 * Test with the number 255:
   * Observe that binary returns 0b11111111 (all 8 bits set to 1).
   * Observe that hexadecimal returns 0xFF (representing two complete nibbles of 15 each).
8. Review Questions & Key Answers
Q1: Why is the Hexadecimal system preferred over Binary when documenting hardware MAC addresses and memory locations?
 * Answer: Hexadecimal is much more concise and easier for humans to read and write without error. Every single Hex digit cleanly represents a group of 4 binary bits, shortening long, unwieldy 48-bit or 64-bit binary strings by a factor of four.
Q2: What is the result of adding binary 1101_2 and 0011_2?
 * Answer: 10000_2 (In decimal: 13 + 3 = 16. Working from right to left: 1+1=0 carry 1; 0+1+1=0 carry 1; 1+0+1=0 carry 1; 1+0+0=1, yielding 10000_2).
Q3: How many unique values can be represented using a single byte (8 bits) of memory?
 * Answer: 256 unique values (calculated as 2^8 = 256, typically ranging from 0 to 255 for unsigned integers, or -128 to +127 in signed Two's Complement notation).

Comments

Popular posts from this blog

विद्यार्थियों के लिए संस्कृत का अत्यंत प्रसिद्ध और महत्वपूर्ण श्लोक है।

MS Excel