Back to Articles
20 min read

Computer Hardware Fundamentals: Digital Logic & Basic Electronics

Before code runs, electrons move. This guide breaks down the foundational layer of computing: how binary data, logic gates, and basic electronics combine to create the digital world.

Binary and hexadecimal number systems

Binary (base-2) uses only 0 and 1 to represent data because electronic circuits have two stable states (on/off), while hexadecimal (base-16, using 0-9 and A-F) provides a compact human-readable representation where each hex digit equals exactly 4 binary bits. These systems are fundamental to computing because all digital data—text, images, instructions—ultimately resolves to binary patterns that hardware can process.

┌──────────────────────────────────────────────────────────────┐ │ NUMBER SYSTEM COMPARISON │ ├──────────────────────────────────────────────────────────────┤ │ │ │ DECIMAL BINARY HEXADECIMAL │ │ (Base 10) (Base 2) (Base 16) │ │ ───────── ────────── ─────────── │ │ 0 0000 0 │ │ 1 0001 1 │ │ 5 0101 5 │ │ 9 1001 9 │ │ 10 1010 A │ │ 15 1111 F │ │ 16 0001 0000 10 │ │ 255 1111 1111 FF │ │ 256 0001 0000 0000 100 │ │ │ │ WHY HEX? 1 hex digit = 4 bits (nibble) │ │ 2 hex digits = 8 bits (byte) │ │ FF = 1111 1111 = 255 (max byte value) │ │ │ │ Color example: #FF5733 │ │ ┌────┬────┬────┐ │ │ │ FF │ 57 │ 33 │ │ │ │Red │Grn │Blu │ │ │ │255 │ 87 │ 51 │ ← Decimal equivalents │ │ └────┴────┴────┘ │ │ │ └──────────────────────────────────────────────────────────────┘
// Number system conversions const decimal = 255; // Decimal to other bases console.log(decimal.toString(2)); // "11111111" (binary) console.log(decimal.toString(16)); // "ff" (hexadecimal) // Parsing from other bases to decimal console.log(parseInt('11111111', 2)); // 255 console.log(parseInt('FF', 16)); // 255 // Practical example: Color manipulation const hexColor = '#FF5733'; const r = parseInt(hexColor.slice(1, 3), 16); // 255 const g = parseInt(hexColor.slice(3, 5), 16); // 87 const b = parseInt(hexColor.slice(5, 7), 16); // 51 // Memory addresses are shown in hex const memoryAddress = 0x7FFF5C3A; // More readable than binary console.log(memoryAddress); // 2147474490 (decimal)

Bits, bytes, and data representation

A bit is the smallest unit of data (0 or 1), while a byte (8 bits) can represent 256 different values (2⁸) and became the standard unit because it's enough to encode one ASCII character. Data hierarchy scales exponentially: kilobyte ( 1,024 bytes), megabyte (1,024 KB), gigabyte (1,024 MB), terabyte (1,024 GB), with all digital information—text, images, audio, video—encoded as specific bit patterns interpreted by software.

┌───────────────────────────────────────────────────────────────┐ │ DATA UNITS HIERARCHY │ ├───────────────────────────────────────────────────────────────┤ │ │ │ BIT (b) 0 or 1 ← Smallest unit │ │ │ │ │ ▼ │ │ NIBBLE 4 bits ← One hex digit │ │ │ ░░░░ │ │ ▼ │ │ BYTE (B) 8 bits ← One ASCII character │ │ │ ░░░░ ░░░░ │ │ ▼ │ │ ┌──────────────────────────────────────────────────────────┐ │ │ │ Unit │ Bytes │ Real-world example │ │ │ ├─────────────┼─────────────────┼──────────────────────────┤ │ │ │ Kilobyte(KB)│ 1,024 │ Short email │ │ │ │ Megabyte(MB)│ 1,048,576 │ MP3 song (~3-5 MB) │ │ │ │ Gigabyte(GB)│ 1,073,741,824 │ HD movie (~4 GB) │ │ │ │ Terabyte(TB)│ ~1 trillion │ 250 HD movies │ │ │ │ Petabyte(PB)│ ~1 quadrillion │ Netflix catalog │ │ │ └─────────────┴─────────────────┴──────────────────────────┘ │ │ │ │ CHARACTER ENCODING EXAMPLE: │ │ Letter 'A' = 65 (decimal) = 0100 0001 (binary) │ │ Letter 'a' = 97 (decimal) = 0110 0001 (binary) │ │ │ └───────────────────────────────────────────────────────────────┘
// Understanding data representation const char = 'A'; const charCode = char.charCodeAt(0); // 65 const binary = charCode.toString(2).padStart(8, '0'); // '01000001' console.log(`'${char}' = ${charCode} decimal = ${binary} binary`); // String to binary representation function stringToBinary(str) { return str.split('').map(char => char.charCodeAt(0).toString(2).padStart(8, '0') ).join(' '); } console.log(stringToBinary('Hi')); // "01001000 01101001" (H=72, i=105) // File size calculations const imageSizeBytes = 2_500_000; // 2.5 million bytes const KB = imageSizeBytes / 1024; // 2441.4 KB const MB = KB / 1024; // 2.38 MB // Why 1024? Because 2^10 = 1024 (power of 2, convenient for binary)

Basic electricity and circuits

Electric current (measured in Amperes) is the flow of electrons through a conductor, driven by voltage (electrical pressure, measured in Volts), while resistance (Ohms) opposes this flow—governed by Ohm's Law: V = I × R. Computers use DC (direct current) at low voltages (typically 3.3V, 5V, 12V), with billions of tiny circuits switching between high voltage (1) and low voltage (0) states billions of times per second to process information.

┌──────────────────────────────────────────────────────────────┐ │ BASIC ELECTRICAL CONCEPTS │ ├──────────────────────────────────────────────────────────────┤ │ │ │ WATER ANALOGY: │ │ ┌──────────────────────────────────────────────┐ │ │ │ VOLTAGE = Water pressure (height) │ │ │ │ CURRENT = Water flow rate │ │ │ │ RESISTANCE = Pipe narrowness │ │ │ │ │ │ │ │ ▓▓▓▓▓▓▓▓▓▓ ← Tank (Voltage source) │ │ │ │ ║ ║ │ │ │ │ ║ ╠════╗ ← Narrow pipe (Resistance) │ │ │ │ ║ ║ ║ │ │ │ │ ▼ Flow ▼ ▼ │ │ │ └──────────────────────────────────────────────┘ │ │ │ │ OHM'S LAW: V = I × R │ │ ─────────────────── │ │ Voltage(V) = Current(A) × Resistance(Ω) │ │ │ │ SIMPLE CIRCUIT: │ │ ┌──────────[R]──────────┐ │ │ │ Resistor │ │ │ (+) (−) │ │ │ ┌───┐ │ │ │ └─────────│ V │─────────┘ │ │ └───┘ │ │ Voltage Source │ │ (Battery) │ │ │ │ PC POWER RAILS: │ │ • +3.3V → Memory, some CPU logic │ │ • +5V → USB, SATA, older components │ │ • +12V → CPU, GPU, drives (main power) │ │ │ └──────────────────────────────────────────────────────────────┘
// Ohm's Law calculations function ohmsLaw({voltage, current, resistance}) { if (voltage === undefined) return {voltage: current * resistance}; if (current === undefined) return {current: voltage / resistance}; if (resistance === undefined) return {resistance: voltage / current}; } // Example: LED circuit const ledCircuit = ohmsLaw({ voltage: 5, // 5V supply (USB) current: 0.020 // 20mA (LED typical) }); console.log(`Need ${ledCircuit.resistance}Ω resistor`); // 250Ω // Power calculation: P = V × I const watts = 12 * 30; // 12V rail at 30A = 360W for GPU

Conductors, insulators, semiconductors

Conductors (copper, gold, aluminum) have free electrons that flow easily, making them ideal for wires and traces; insulators (rubber, plastic, glass) resist electron flow and protect us from shocks. Semiconductors (silicon, germanium) are the magic middle—their conductivity can be precisely controlled by adding impurities (doping), enabling the on/off switching behavior essential for transistors that form the basis of all computer chips.

┌───────────────────────────────────────────────────────────────┐ │ CONDUCTIVITY SPECTRUM │ ├───────────────────────────────────────────────────────────────┤ │ │ │ CONDUCTOR SEMICONDUCTOR INSULATOR │ │ (Low resistance) (Controllable) (High resistance) │ │ │ │ ●●●●●●●●→ ●●●→ or ●●●●●→ ●→ (barely) │ │ Easy flow Controlled flow Blocked flow │ │ │ │ Examples: Examples: Examples: │ │ • Copper • Silicon (Si) • Rubber │ │ • Gold • Germanium (Ge) • Glass │ │ • Aluminum • Gallium Arsenide • Plastic │ │ │ ├───────────────────────────────────────────────────────────────┤ │ SEMICONDUCTOR DOPING: │ │ │ │ Pure Silicon N-Type P-Type │ │ (4 electrons) (Extra electron) (Missing electron) │ │ │ │ Si Si Si │ │ /│\ /│\● /│\○ │ │ Si-Si-Si Si-Si-Si Si-Si-Si │ │ \│/ \│/ \│/ │ │ Si Si Si │ │ │ │ ● = extra electron (donor: Phosphorus) │ │ ○ = missing electron/"hole" (acceptor: Boron) │ │ │ │ N-type + P-type junction = DIODE/TRANSISTOR │ │ │ └───────────────────────────────────────────────────────────────┘
// Conceptual model of conductivity const materials = { copper: {type: 'conductor', resistivity: 1.68e-8}, silicon: {type: 'semiconductor', resistivity: 2.3e3}, glass: {type: 'insulator', resistivity: 1e12} }; // Conductivity = 1 / Resistivity Object.entries(materials).forEach(([name, props]) => { const conductivity = 1 / props.resistivity; console.log(`${name}: ${conductivity.toExponential(2)} S/m`); }); // Why silicon? Its conductivity can be changed 1,000,000x by doping // This allows us to create switches (transistors)

Logic gates (AND, OR, NOT, XOR, NAND, NOR)

Logic gates are the fundamental building blocks of digital circuits, implementing Boolean operations on binary inputs—AND outputs 1 only when all inputs are 1, OR outputs 1 when any input is 1, NOT inverts the input. NAND (NOT-AND) is considered "universal" because any other gate can be built from NAND gates alone, which is why it's the basis of most integrated circuits; a modern CPU contains billions of these gates working together.

┌────────────────────────────────────────────────────────────────┐ │ LOGIC GATES │ ├────────────────────────────────────────────────────────────────┤ │ │ │ AND OR NOT │ │ A ──┐ A ──┐ A ──▷○── Y │ │ │&──── Y │≥1── Y (inverter) │ │ B ──┘ B ──┘ │ │ │ │ A B │ Y A B │ Y A │ Y │ │ ────┼── ────┼── ──┼── │ │ 0 0 │ 0 0 0 │ 0 0 │ 1 │ │ 0 1 │ 0 0 1 │ 1 1 │ 0 │ │ 1 0 │ 0 1 0 │ 1 │ │ 1 1 │ 1 1 1 │ 1 │ │ │ ├────────────────────────────────────────────────────────────────┤ │ │ │ NAND (universal) NOR XOR │ │ A ──┐ A ──┐ A ──┐ │ │ │&○── Y │≥1○── Y │=1── Y │ │ B ──┘ B ──┘ B ──┘ │ │ │ │ A B │ Y A B │ Y A B │ Y │ │ ────┼── ────┼── ────┼── │ │ 0 0 │ 1 0 0 │ 1 0 0 │ 0 │ │ 0 1 │ 1 0 1 │ 0 0 1 │ 1 │ │ 1 0 │ 1 1 0 │ 0 1 0 │ 1 │ │ 1 1 │ 0 1 1 │ 0 1 1 │ 0 │ │ │ │ ○ = inversion (bubble) │ │ NAND = NOT(AND), NOR = NOT(OR) │ │ XOR = "exclusive or" (different inputs = 1) │ │ │ └────────────────────────────────────────────────────────────────┘
// Logic gate implementations const gates = { AND: (a, b) => a & b, OR: (a, b) => a | b, NOT: (a) => a ? 0 : 1, XOR: (a, b) => a ^ b, NAND: (a, b) => (a & b) ? 0 : 1, NOR: (a, b) => (a | b) ? 0 : 1 }; // Truth table generator function truthTable(gate) { console.log(`${gate} truth table:`); const fn = gates[gate]; if (gate === 'NOT') { [0, 1].forEach(a => console.log(` ${a}${fn(a)}`)); } else { [[0, 0], [0, 1], [1, 0], [1, 1]].forEach(([a, b]) => console.log(` ${a} ${b}${fn(a, b)}`) ); } } // Building complex operations from NAND (universal gate) function NAND(a, b) { return (a && b) ? 0 : 1; } function NOT_from_NAND(a) { return NAND(a, a); } function AND_from_NAND(a, b) { return NAND(NAND(a, b), NAND(a, b)); } function OR_from_NAND(a, b) { return NAND(NAND(a, a), NAND(b, b)); } // XOR is useful for: parity checking, encryption, binary addition console.log(5 ^ 3); // 6: Used in simple encryption

Boolean algebra fundamentals

Boolean algebra, developed by George Boole in 1854, provides the mathematical foundation for digital logic, using variables that are only TRUE (1) or FALSE (0) with operations AND (·), OR (+), and NOT (¬). Key laws like De Morgan's theorems (¬(A·B) = ¬A+¬B) enable simplification of complex logic circuits, reducing transistor count—this optimization is essential in chip design where billions of gates must be minimized for power efficiency and speed.

┌───────────────────────────────────────────────────────────────┐ │ BOOLEAN ALGEBRA LAWS │ ├───────────────────────────────────────────────────────────────┤ │ │ │ BASIC IDENTITIES: │ │ ───────────────── │ │ A · 0 = 0 A + 0 = A (Identity) │ │ A · 1 = A A + 1 = 1 (Domination) │ │ A · A = A A + A = A (Idempotent) │ │ A · Ā = 0 A + Ā = 1 (Complement) │ │ │ │ KEY LAWS: │ │ ───────── │ │ A·B = B·A A+B = B+A (Commutative) │ │ A·(B·C) = (A·B)·C A+(B+C) = (A+B)+C (Associative) │ │ A·(B+C) = A·B+A·C A+(B·C) = (A+B)·(A+C) (Distributive) │ │ │ │ DE MORGAN'S THEOREMS (crucial for circuit optimization): │ │ ────────────────────────────────────────────────────── │ │ ¬(A · B) = ¬A + ¬B (NOT-AND = OR of NOTs) │ │ ¬(A + B) = ¬A · ¬B (NOT-OR = AND of NOTs) │ │ │ │ EXAMPLE SIMPLIFICATION: │ │ ─────────────────────── │ │ F = A·B + A·B̄ + Ā·B │ │ = A·(B + B̄) + Ā·B ← Factor out A │ │ = A·1 + Ā·B ← B + B̄ = 1 │ │ = A + Ā·B ← A·1 = A │ │ = A + B ← Absorption law │ │ │ │ Original: 3 ANDs + 2 ORs → Simplified: 1 OR │ │ (Fewer gates = less power, faster, cheaper) │ │ │ └───────────────────────────────────────────────────────────────┘
// Boolean algebra in JavaScript const A = true, B = false; // Basic operations const AND = A && B; // false const OR = A || B; // true const NOT_A = !A; // false // De Morgan's Theorems demonstration const expr1 = !(A && B); // NOT(A AND B) const expr2 = (!A) || (!B); // (NOT A) OR (NOT B) console.log(expr1 === expr2); // true: De Morgan's theorem // Practical example: Simplifying conditions // Before: (isAdmin && hasPermission) || (isAdmin && !hasPermission) // After (simplified): isAdmin const isAdmin = true; const hasPermission = false; const complex = (isAdmin && hasPermission) || (isAdmin && !hasPermission); const simplified = isAdmin; console.log(complex === simplified); // true // XOR via Boolean algebra: A XOR B = A·B̄ + Ā·B function XOR(a, b) { return (a && !b) || (!a && b); }