Skip to content

Commit e466fda

Browse files
committed
CSO notes
1 parent 1df4334 commit e466fda

17 files changed

Lines changed: 2241 additions & 5 deletions

js/mathjax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
window.MathJax = {
22
loader: { load: ['[tex]/textmacros'] },
33
tex: {
4-
packages: { '[+]': ['noerrors', 'ams', 'textmacros'] },
4+
packages: { '[+]': ['noerrors', 'ams', 'textmacros', 'amssymb'] },
55
macros: {
66
qed: "\\square",
77
ran: "\\text{ran}",
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---
2+
title: Number Systems & Memory Organization
3+
date:
4+
---
5+
6+
## Number system fundamentals
7+
8+
Digital systems use several bases to represent information:
9+
10+
- **Decimal (base 10):** digits $0$–$9$, human-friendly.
11+
- **Binary (base 2):** digits $0$–$1$, directly matches on/off electrical signals in hardware.
12+
- **Hexadecimal (base 16):** digits $0$–$9$ and $A$–$F$ (representing $10$–$15$), a compact way to write binary.
13+
- Other bases (e.g., ternary base $3$, quaternary base $4$) are conceptually similar.
14+
15+
All positional number systems use powers of the base:
16+
17+
- Rightmost digit is position $0$, next is position $1$, etc.
18+
- Value $=$ sum over $\text{digit} \times \text{base}^{\text{position}}$.
19+
20+
Example (binary $1011_2$):
21+
22+
$$
23+
1011_2 = 1 \cdot 2^3 + 0 \cdot 2^2 + 1 \cdot 2^1 + 1 \cdot 2^0
24+
= 8 + 0 + 2 + 1 = 11_{10}.
25+
$$
26+
27+
## Base conversions
28+
29+
### To decimal (positional expansion)
30+
31+
- **Binary -> decimal:** multiply each bit by the corresponding power of $2$ and add.
32+
- **Hex -> decimal:** multiply each hex digit by the corresponding power of $16$ and add.
33+
34+
Example:
35+
36+
$$
37+
(10110)_2 = 1 \cdot 2^4 + 0 \cdot 2^3 + 1 \cdot 2^2 + 1 \cdot 2^1 + 0 \cdot 2^0 = 22_{10}.
38+
$$
39+
40+
$$
41+
(\text{AE3})_{16} = 10 \cdot 16^2 + 14 \cdot 16^1 + 3 \cdot 16^0 = 2787_{10}.
42+
$$
43+
44+
### From decimal (repeated division)
45+
46+
Use repeated division by the target base, tracking remainders:
47+
48+
- **Decimal -> binary:** divide by $2$ until the quotient is $0$, read remainders bottom-to-top.
49+
- **Decimal -> hex:** same idea but divide by $16$.
50+
51+
Example:
52+
53+
- $20_{10} \to$ binary: remainders give $(10100)_2$.
54+
- $20_{10} \to$ hex: remainders give $(14)_{16}$.
55+
56+
### Direct binary–hex mapping
57+
58+
Because $16 = 2^4$, $4$ binary bits correspond to $1$ hex digit:
59+
60+
- **Binary -> hex:** group bits into $4$-bit chunks from the right, pad with leading zeros if needed, convert each chunk to hex.
61+
- **Hex -> binary:** replace each hex digit with its $4$-bit binary equivalent.
62+
63+
Example:
64+
65+
- $(110100)_2 \to 0011\,0100 \to (34)_{16}$.
66+
- $(\text{B2F})_{16} \to 1011\,0010\,1111_2$.
67+
68+
A small table (for $0$–$15$) links decimal, hex, and $4$-bit binary.
69+
70+
## Data representation
71+
72+
### Bits, bytes, and larger units
73+
74+
- $1$ **bit** $=$ $1$ binary digit ($0$ or $1$).
75+
- $1$ **byte** $=$ $8$ bits.
76+
- $1$ **KB** $=$ $2^{10}$ bytes $= 1024$ bytes.
77+
- $1$ **MB** $=$ $2^{10}$ KB.
78+
- $1$ **GB** $=$ $2^{10}$ MB.
79+
- $1$ **TB** $=$ $2^{10}$ GB.
80+
81+
An $n$-bit quantity can represent $2^n$ distinct values:
82+
83+
- $1$ bit -> $2$ values.
84+
- $8$ bits ($1$ byte) -> $2^8 = 256$ values.
85+
86+
### Text encoding
87+
88+
- **ASCII:** uses $1$ byte ($8$ bits) per character; can encode up to $256$ characters (e.g., `'A' = 65_{10}$).
89+
- **Unicode:** uses $16$ bits or more; supports many languages and symbols ($2^{16}$ or more code points).
90+
91+
### Numbers
92+
93+
- **Integers:** stored as binary whole numbers (with specific schemes for signed vs unsigned).
94+
- **Floating-point:** used for real numbers; stored using standardized formats (e.g., IEEE-754) with finite precision, so not all real values can be represented exactly.
95+
96+
## Memory organization
97+
98+
### Addresses vs contents
99+
100+
Memory is a collection of storage cells:
101+
102+
- **Memory address:** where data is stored (an index/location).
103+
- **Memory content:** the bits actually stored at that address.
104+
105+
For an $n$-bit memory **location**:
106+
107+
- It can store one of $2^n$ different values (because the content has $n$ bits).
108+
109+
For an $n$-bit **address**:
110+
111+
- It can point to $2^n$ distinct locations.
112+
113+
Example:
114+
115+
- $32$-bit data cell -> $2^{32}$ possible contents.
116+
- $16$-bit address -> $2^{16} = 64\text{K}$ addressable locations.
117+
118+
### Size and powers of two
119+
120+
Useful powers of two:
121+
122+
- $2^{10} = 1024 \approx 1\text{K}$
123+
- $2^{20} \approx 1\text{M}$
124+
- $2^{30} \approx 1\text{G}$
125+
- $2^{40} \approx 1\text{T}$
126+
127+
Key rules:
128+
129+
$$
130+
2^n \cdot 2^m = 2^{n+m}, \quad \log_2(nm) = \log_2(n) + \log_2(m).
131+
$$
132+
133+
Examples:
134+
135+
- $2^{16} = 2^{10} \cdot 2^6 = 1\text{K} \cdot 64 = 64\text{K}$.
136+
- If a system has a $24$-bit address bus, it can address $2^{24}$ bytes $= 16\text{MB}$.
137+
138+
### Basic computer architecture
139+
140+
A simplified view:
141+
142+
- **CPU** connects to **RAM** using three buses:
143+
- **Address bus:** selects which memory location to access.
144+
- **Data bus:** carries data between CPU and memory.
145+
- **Control bus:** carries control signals (read/write, etc.).
146+
147+
The width of the address bus determines the maximum directly addressable memory.
148+
149+
## Practice and examples
150+
151+
The note includes worked examples on:
152+
153+
1. **Base conversions:**
154+
- Binary <-> decimal.
155+
- Hex <-> decimal.
156+
- Decimal <-> binary via repeated division.
157+
- Hex <-> binary via $4$-bit grouping.
158+
159+
2. **Memory calculations:**
160+
- Bits needed to address a given memory size (e.g., $8\text{MB}$ requires $23$ address bits).
161+
- Maximum memory size from a given address width (e.g., $24$-bit address bus -> $16\text{MB}$).
162+
163+
3. **Data values in $n$-bit locations:**
164+
- Counting the number of distinct values (e.g., a $12$-bit cell stores $2^{12} = 4096$ values).
165+
166+
## Quick reference
167+
168+
Common facts:
169+
170+
- $1$ byte $= 8$ bits.
171+
- $1$ KB $= 1024$ bytes.
172+
- $1$ MB $= 1024$ KB.
173+
- $1$ GB $= 1024$ MB.
174+
- $1$ TB $= 1024$ GB.
175+
176+
Hex <-> $4$-bit binary shortcuts:
177+
178+
- $A = 1010$, $B = 1011$, $C = 1100$, $D = 1101$, $E = 1110$, $F = 1111$.
179+
180+
Frequent mistakes to avoid:
181+
182+
1. Confusing memory **addresses** with memory **contents**.
183+
2. Forgetting to pad binary with leading zeros when grouping for hex.
184+
3. Using $1000$ instead of $1024$ for KB/MB/GB in CS contexts.
185+
4. Reading division remainders in the wrong order in base conversion.
186+
5. Misplacing powers of the base in positional notation.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
title: Data Representation
3+
date:
4+
---
5+
6+
## Text and character encoding
7+
8+
Computers store text as numeric codes.
9+
10+
### ASCII
11+
12+
- 7-bit core set (values 0 to 127), extended ASCII uses 8 bits (0 to 255).
13+
- Each character is mapped to an integer code, then stored in binary.
14+
15+
Example: "Hi"
16+
17+
- 'H' is 72 in decimal, which is $01001000_2$.
18+
- 'i' is 105 in decimal, which is $01101001_2$.
19+
- "Hi" in binary is $01001000\ 01101001$.
20+
21+
### Unicode and UTF-8
22+
23+
- Unicode assigns code points to characters from many languages and symbol sets.
24+
- UTF-8 encodes each code point using 1 to 4 bytes.
25+
- All ASCII bytes keep their original values in UTF-8, so ASCII text is valid UTF-8.
26+
27+
## Integer representation
28+
29+
Let an integer be stored in $n$ bits.
30+
31+
### Unsigned integers
32+
33+
- Represent non-negative values only.
34+
- Bit pattern $b_{n-1} \dots b_1 b_0$ is interpreted as
35+
36+
$$
37+
b_{n-1} 2^{n-1} + \dots + b_1 2^1 + b_0 2^0.
38+
$$
39+
40+
- Range: $0$ to $2^n - 1$.
41+
42+
### Signed integers
43+
44+
Several conventions exist to represent negative numbers.
45+
46+
#### Sign-magnitude
47+
48+
- Most significant bit (MSB) is the sign: 0 for positive, 1 for negative.
49+
- Remaining bits store the magnitude.
50+
- Problem: there are two representations of zero ($+0$ and $-0$).
51+
52+
Example with 3 bits:
53+
54+
- $000$ is $+0$.
55+
- $011$ is $+3$.
56+
- $100$ is $-0$.
57+
- $111$ is $-3$.
58+
59+
#### One's complement
60+
61+
- Negative numbers are formed by flipping all bits of the positive value.
62+
- Still has two zeros ($+0$ and $-0$).
63+
- Range for $n$ bits: from $-(2^{n-1} - 1)$ to $+(2^{n-1} - 1)$.
64+
65+
#### Two's complement
66+
67+
This is the standard representation.
68+
69+
- To get $-x$ from $x$:
70+
1. Write $x$ in binary.
71+
2. Flip all bits (one's complement).
72+
3. Add $1$.
73+
74+
- Range for $n$ bits: $-2^{n-1}$ to $2^{n-1} - 1$.
75+
- There is a unique zero and integer addition works uniformly at the bit level.
76+
77+
Example: $-7$ in 8-bit two's complement.
78+
79+
- $+7 = 00000111_2$
80+
- Flip bits: $11111000_2$
81+
- Add one: $11111001_2$
82+
- So $-7 = 11111001_2$.
83+
84+
Check: $11111001_2 + 00000111_2 = 1 00000000_2$, and the carry out of the top bit is ignored, leaving zero.
85+
86+
## Floating point representation
87+
88+
Modern systems use IEEE 754 formats.
89+
90+
### Single precision (32-bit)
91+
92+
Written as
93+
94+
- 1 sign bit,
95+
- 8-bit exponent with bias 127,
96+
- 23-bit fraction (mantissa) with an implicit leading bit for normalized values.
97+
98+
If the stored fields are $s$ (sign), $e$ (exponent), $f$ (fraction bits), then for a normalized number
99+
100+
$$
101+
\text{value} = (-1)^s \times (1.f)_2 \times 2^{e - 127}.
102+
$$
103+
104+
Example: $5.75$.
105+
106+
1. Convert to binary: $5.75 = 101.11_2$.
107+
2. Normalize: $101.11_2 = 1.0111_2 \times 2^2$.
108+
3. Sign bit $s = 0$.
109+
4. Exponent field $e = 2 + 127 = 129 = 10000001_2$.
110+
5. Fraction field holds $0111$ followed by zeros.
111+
112+
So the bit pattern is
113+
114+
115+
$$
116+
0\ 10000001\ 01110000000000000000000.
117+
$$
118+
119+
120+
### Key floating point ideas
121+
122+
- Only finitely many real numbers can be represented, so most reals are approximated.
123+
- Spacing between representable numbers grows as magnitudes grow.
124+
- Rounding means algebraic laws like associativity may fail, so order of operations matters in numerical code.

0 commit comments

Comments
 (0)