MEG 202 · Basic Software Engineering
Our notes
2 min readWhy binary
Electronic circuits are most reliable with two states (on/off, high/low voltage), so computers use binary (base 2). A bit is one binary digit; a byte is 8 bits; memory sizes: 1 KiB = 1,024 bytes (manufacturers often use 1 kB = 1,000).
Number bases
| Decimal | Binary | Octal | Hexadecimal |
|---|---|---|---|
| 10 | 1010 | 12 | A |
| 15 | 1111 | 17 | F |
| 47 | 101111 | 57 | 2F |
| 255 | 11111111 | 377 | FF |
Hexadecimal (base 16, digits 0-9 and A-F) is a compact way to write binary: each hex digit is 4 bits.
Conversions
- Decimal to binary: divide repeatedly by 2 and read remainders from bottom to top. 45 → 45/2 = 22 r1, 22/2 = 11 r0, 11/2 = 5 r1, 5/2 = 2 r1, 2/2 = 1 r0, 1/2 = 0 r1 → 101101.
- Binary to decimal: add place values. 101101 = 32 + 8 + 4 + 1 = 45.
- Fractions: multiply by 2 and take the integer parts. 0.625 → 1.25 (1), 0.5 (0), 1.0 (1) → 0.101. So 13.625 = 1101.101.
- Binary to hex: group bits in fours from the right: 0010 1111 = 2F.
Signed integers: two's complement
In an n-bit two's complement number, the leftmost bit has weight −2ⁿ⁻¹. To get −x: write x in binary, invert all bits, add 1.
Example (8 bits): +5 = 00000101 → invert 11111010 → add 1 → 11111011 = −5. Range for 8 bits: −128 to +127.
Subtraction becomes addition: 45 − 5 = 45 + (−5): 00101101 + 11111011 = (1)00101000. Discard the carry out of bit 8: 00101000 = 40 ✓.
Overflow: if two positives give a negative (or two negatives a positive), the result is out of range: 100 + 50 in 8 bits gives −106.
Binary addition rules
0 + 0 = 0; 0 + 1 = 1; 1 + 1 = 10 (0 carry 1); 1 + 1 + 1 = 11 (1 carry 1).
Floating point
Real numbers are stored like scientific notation: value = (−1)^sign × 1.fraction × 2^(exponent − bias). IEEE 754 single precision: 1 sign bit, 8 exponent bits (bias 127), 23 fraction bits; double precision: 1, 11 (bias 1,023), 52.
Example: 13.625 = 1101.101₂ = 1.101101 × 2³. Sign 0; exponent 3 + 127 = 130 = 10000010; fraction 10110100...0.
Errors in computer arithmetic
- Round-off error: many decimals have no exact binary form. 0.1 in binary is 0.000110011... repeating, so 0.1 + 0.2 in most languages gives 0.30000000000000004. Never test real numbers for exact equality; test whether |a − b| is below a small tolerance.
- Overflow and underflow: numbers too large or too small for the format.
- Truncation error: from cutting off an infinite process (a series, a numerical integration step).
- Loss of significance: subtracting nearly equal numbers loses digits.
Exams usually ask
- Convert between decimal, binary, octal and hex, including fractions.
- Represent negative numbers in two's complement and perform subtraction.
- Explain floating-point representation and round-off error.
Common mistake
Forgetting to add 1 after inverting the bits, which gives the one's complement instead of the two's complement.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick Introduction to Computer Science, 5.3 Machine-Level Information Representation
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 5.3 Machine-Level Information Representation, CC BY-NC-SA 4.0 · licence
-
Binary number
(opens en.wikipedia.org in a new tab)
Wikipedia, Binary number, CC BY-SA 4.0 · licence
-
Hexadecimal
(opens en.wikipedia.org in a new tab)
Wikipedia, Hexadecimal, CC BY-SA 4.0 · licence
-
Two's complement
(opens en.wikipedia.org in a new tab)
Wikipedia, Two's complement, CC BY-SA 4.0 · licence
-
NOUN CIT 317: Computer Architecture and Organization
NOUN course book (PDF, about 2.7 MB). Processor, memory and number representation.
(opens nou.edu.ng in a new tab)
-
NOUN CIT 104: Introduction to Computers
NOUN course book (PDF, about 3 MB). Hardware, software and packages.
(opens nou.edu.ng in a new tab)
-
OpenStax, Introduction to Computer Science (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Introduction to Computer Science (whole free textbook), CC BY-NC-SA 4.0 · licence
Watch
-
Number Systems Introduction - Decimal, Binary, Octal & Hexadecimal
11 min · ≈ 50 MB · about ₦28
(opens youtube.com in a new tab)
-
Introduction to Number Systems
11 min · ≈ 50 MB · about ₦28
(opens youtube.com in a new tab)
-
Representing Numbers and Letters with Binary: Crash Course Computer Science #4
11 min · ≈ 50 MB · about ₦28
(opens youtube.com in a new tab)
-
Number System Conversion - Decimal to Binary | Computer Basics Lecture Series
8 min · ≈ 36 MB · about ₦20
(opens youtube.com in a new tab)
-
Computer Number System | Binary/ Decimal/ Octal/ Hexadecimal | All Conversion in One Shot | CTA
31 min · ≈ 140 MB · about ₦79
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.