CS core subjects

Sefism.

Chapter 01

How a computer runs a program

Bits, logic, the CPU, memory and the path from source code to running instructions. Every later chapter sits on this picture.

Binary and hexadecimal

Also called base 2, base 16, hex, hexadecimal

What
Number systems with base 2 and base 16. Each hex digit stands for exactly four bits, so hex is a compact way to write binary values.
Why
Memory addresses, colours, permissions, packet dumps and bit flags are all written in binary or hex. Converting by hand builds the intuition to read them.
Example
Decimal 202 is binary 1100 1010, which is hex CA: 1100 is C and 1010 is A.

Watch out

Forgetting that a fixed number of bits holds a fixed range, which is where overflow comes from.

Two's complement

What
The standard way to store signed integers: to negate a number, invert every bit and add one. The top bit acts as the sign.
Why
It lets the same adder circuit handle positive and negative numbers, and it explains why signed values wrap from the maximum to the minimum on overflow.
Code
  5  = 0000 0101
 invert 1111 1010
  + 1 = 1111 1011  = -5

 8-bit range: -128 to 127
 127 + 1 wraps to -128

Watch out

Assuming integer overflow raises an error; in C, Java and many others it silently wraps or is undefined.

Character encoding

Also called ASCII, Unicode, UTF-8

What
The mapping from characters to numbers and bytes. Unicode gives every character a code point; UTF-8 stores each code point in one to four bytes, keeping ASCII as one byte.
Why
Every string, file and database column has an encoding. Getting it wrong produces garbled text and broken length checks for any language beyond English.
Example
The letter A is one byte in UTF-8, the Urdu letter ب is two bytes, and most emoji are four.

Watch out

Treating string length in bytes as the number of characters.

Floating point

Also called IEEE 754, float, double

What
The binary format for real numbers: a sign, an exponent and a fraction. Most decimal fractions, such as 0.1, cannot be stored exactly.
Why
It explains rounding surprises and why money is stored as integer cents or a decimal type, and why floats are compared with a small tolerance.
Python
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1 + 0.2 == 0.3
False
>>> abs((0.1 + 0.2) - 0.3) < 1e-9
True

Watch out

Storing prices or balances in a float column.

Logic gates

Also called AND, OR, NOT, NAND, XOR

What
Tiny circuits that compute a boolean function of their inputs. Every other digital circuit, from adders to memory, is built by combining them.
Why
They show that a computer is layers of simple parts. A half adder, for example, is one XOR gate for the sum and one AND gate for the carry.

Watch out

Skipping this layer and never seeing how instructions become physical operations.

CPU, registers and ALU

Also called processor, arithmetic logic unit, program counter, CPU, ALU, register

What
The CPU executes instructions. Registers are its few, very fast storage slots; the ALU does arithmetic and logic; the control unit steers each step; the program counter holds the next instruction address.
Why
Knowing these parts lets you read assembly, understand what a context switch saves, and see why keeping data in registers and caches is fast.

Watch out

Picturing the CPU working directly on variables in main memory for every operation.

Fetch, decode, execute

Also called instruction cycle, fetch-decode-execute cycle

What
The loop every CPU runs: fetch the instruction at the program counter, decode it, execute it, then move to the next instruction or jump.
Why
High-level code compiles into long sequences of these simple steps, which is why one line of code can take many instructions.
Code
loop:  LOAD  R1, [i]
       ADD   R2, R2, R1
       ADD   R1, R1, 1
       STORE [i], R1
       CMP   R1, 10
       JLE   loop

Memory hierarchy

Also called cache, locality, L1, L2, L3

What
Storage layers from registers and CPU caches to RAM, SSD and network, each much larger and much slower than the one above.
Why
Caches work because of locality: programs reuse recent data and read nearby addresses. Code that respects this, such as scanning arrays in order, runs much faster.
Example
Rough latencies: register under 1 ns, L1 cache about 1 ns, RAM about 100 ns, SSD read about 100 microseconds, a cross-continent round trip about 150 ms.

Watch out

Counting operations only and ignoring where the data lives.

Stack and heap

Also called stack frame, stack overflow, call stack

What
Two memory regions of a running program. The stack holds call frames with locals and return addresses; the heap holds data allocated at run time with a flexible lifetime.
Why
It explains stack overflows from deep recursion, memory leaks, dangling pointers, and what a garbage collector actually manages.

Watch out

Returning a pointer to a local variable in C, which points into a frame that no longer exists.

Compiler, interpreter and JIT

Also called bytecode, virtual machine, JVM, JIT, just-in-time compiler, Java virtual machine

What
A compiler translates a whole program to machine code ahead of time; an interpreter executes it as it reads. Many languages compile to bytecode run by a virtual machine with a just-in-time compiler.
Why
It decides when errors appear, how fast code starts and runs, and what must be installed to run the program.

Watch out

Calling a language compiled or interpreted; that is a property of the implementation.

01 · How a computer runs a program1 / 9