UNIHIVE

Study Hub UNIPORT MEG 202

MEG 202 · Basic Software Engineering

Number systems and computer arithmetic

Binary, octal and hexadecimal, conversions, two's complement, binary addition, floating point and its errors.

Our notes

2 min read

Why 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

DecimalBinaryOctalHexadecimal
10101012A
15111117F
47101111572F
25511111111377FF

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

Watch

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.

All MEG 202 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.