First Year Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Number systems and codes

Place-value number systems (binary, octal, decimal, hex), conversions, complements and arithmetic, and commonly used digital codes (BCD, Excess-3, Gray, ASCII) with error-detecting ideas (parity, Hamming).

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Digital systems store and process all information as numbers and codes. An engineer uses number systems to represent data, addresses, and control signals, and uses codes to make data human-readable, compatible across devices, and robust against errors. You need this topic before Logic gates and Boolean algebra, Combinational circuits, and later topics like Microcontroller basics and Communication systems, because every bit pattern has meaning only through a chosen number system or code.

Key ideas

  • Radix (base): The number of unique digits including zero in a system. Decimal has base 10 (digits 0–9), binary base 2 (0,1), octal base 8 (0–7), hexadecimal base 16 (0–9, A–F).
  • Positional (place‑value) notation: Any number in base r is written as N = d_{n−1}…d_1 d_0 . d_{−1} d_{−2}…, where the value is Σ d_i r^i. Each position i has weight r^i. Assumptions: 0 ≤ d_i ≤ r−1 and only finitely many non-zero digits.
  • Binary number: A number represented in base 2. Each binary digit is a bit. Bits group into nibbles (4 bits), bytes (8 bits), words (commonly 16/32/64 bits depending on architecture).
  • Octal number: Base 8. Often used as a compact form for binary, since 1 octal digit = 3 binary bits.
  • Hexadecimal number: Base 16. Common in engineering because 1 hex digit = 4 binary bits, aligning with nibble boundaries.
  • Fixed-point fraction: A fractional number with a fixed binary (or other base) point position. Value is Σ d_i r^i for negative i as well (i = −1, −2, …) representing fractional weights. Note: Many decimal fractions (e.g., 0.1₁₀) have repeating binary expansions, so practical fixed-point representations use rounding or truncation.
  • Unsigned representation: All bit patterns represent non-negative integers, from 0 to 2^n − 1 for n bits.
  • Signed magnitude: Leftmost bit is sign (0 for +, 1 for −), remaining bits give magnitude. Two zeros (+0 and −0) exist; rarely used in hardware arithmetic.
  • 1’s complement (for binary): Negative numbers are represented by complementing each bit of the positive magnitude. Has +0 and −0; subtraction uses end-around carry.
  • 2’s complement: Negative numbers are represented by complementing each bit and adding 1 to the least significant position. Unique zero and simple addition/subtraction; widely used in digital systems. When widening to more bits, sign-extend (replicate the sign bit) to preserve the value.
  • (r−1)’s and r’s complements: For base r numbers, (r−1)’s complement is formed by digit-wise subtraction from r−1; r’s complement is (r−1)’s complement + 1. Used to perform subtraction as addition of a complement.
  • Range of n-bit 2’s complement: From −2^{n−1} to 2^{n−1} − 1. Overflow occurs when the true result is outside this range; detected when carries into and out of MSB differ. For unsigned addition, overflow is indicated by a carry out of the MSB (carry-out = 1).
  • BCD (Binary Coded Decimal): A family of decimal codes where each decimal digit is encoded separately in binary. The common weighted code is 8421 BCD: digit 0–9 represented as its 4-bit binary (0000 to 1001). Digit values 10–15 are invalid in 8421 BCD.
  • Excess‑3 code (XS‑3): A self-complementing decimal code where each decimal digit is encoded as its 8421 value plus 3 (0011). It avoids 0000 and 1111 and simplifies 9’s complement operations.
  • Gray code (reflected binary code): An ordering of 2^n binary code words such that adjacent values differ by only one bit (Hamming distance 1). Reduces errors in position encoders and ADC timing.
  • ASCII (American Standard Code for Information Interchange): A 7-bit character code (commonly stored in 8-bit bytes with a leading 0) representing letters, digits, punctuation, and control characters. Examples: '0' = 48₁₀ = 30₁₆, 'A' = 65₁₀ = 41₁₆, 'a' = 97₁₀ = 61₁₆.
  • Parity bit: A single error-detection bit added to data to make the count of 1s even (even parity) or odd (odd parity). Detects all single-bit errors, misses some multi-bit errors.
  • Hamming distance: The number of bit positions in which two code words differ. A code with minimum distance d_min detects up to d_min − 1 bit errors and corrects up to ⌊(d_min − 1)/2⌋ errors.
  • Hamming (7,4) code: A single-error-correcting code that maps 4 data bits to 7 bits by adding 3 parity bits placed at positions 1, 2, and 4 (powers of 2). General parity count: find the smallest m such that 2^m ≥ m + k + 1 for k data bits.

Typical derivations universities ask:

  1. Expansion method (base‑r value formula): For N = d_{n−1}…d_0.d_{−1}… in base r, Value = d_{n−1} r^{n−1} + … + d_0 r^0 + d_{−1} r^{−1} + … This is a direct consequence of place weights in positional notation.
  2. Base conversion (decimal ↔ binary):
    • Integer part: repeated division by base r and read remainders upward.
    • Fractional part: repeated multiplication by r and read integer parts downward. Proof idea: Each division by r extracts the least significant digit as a remainder; each multiplication by r extracts the next fractional digit as the integer part.
  3. r’s complement subtraction algorithm: To compute A − B in base r on fixed width n digits:
    • Form r’s complement of B (B_c = r^n − B).
    • Compute S = A + B_c.
    • If there is an end carry, discard it; the result is positive and correct. If not, take r’s complement of S and mark negative. A standard proof uses modular arithmetic modulo r^n.
  4. 2’s complement range: For n bits, negative values represent numbers r^n − |x| with r = 2. Smallest is when MSB = 1 and all others 0 → value −2^{n−1}. Largest is when MSB = 0 and all others 1 → 2^{n−1} − 1.
  5. Gray↔binary conversion:
    • Binary to Gray: g_{n−1} = b_{n−1}; g_i = b_{i+1} XOR b_i for i = n−2 … 0.
    • Gray to Binary: b_{n−1} = g_{n−1}; b_i = b_{i+1} XOR g_i. Derivation: Use the one-bit change property and cumulative XOR.

What to draw and label (if asked):

  • Place-value table for base r: a row of labeled columns with weights …, r^3, r^2, r^1, r^0, r^{−1}, r^{−2}, … and example digits placed under them.
  • Bit-grouping diagram for binary↔octal (group bits in 3s) and binary↔hex (group bits in 4s), showing LSB-side grouping with leading zeros added if needed.
  • Gray code sequence for 3 or 4 bits: a table listing binary and corresponding Gray codes side by side, highlighting single-bit transitions.
  • Hamming (7,4) layout: positions 1..7 with parity at 1,2,4 and data at 3,5,6,7; show parity coverage sets: p1 → positions with LSB of index = 1; p2 → positions with second bit of index = 1; p4 → positions with third bit of index = 1.

Formulas

N = Σ (d_i · r^i) — Positional value; d_i is the digit at position i (unitless), r is base (unitless), i is integer index; applies to any base‑r positional system including fractions (i negative to the right of point). r’s complement of X (n digits) = r^n − X — X is the number (unitless) in base r, n is the fixed digit width; used for subtraction by addition. (r−1)’s complement of X (n digits) = (r^n − 1) − X — Used for alternate complement subtraction; add an end-around carry if produced. 2’s complement of X (binary) = (bitwise NOT of X) + 1 — X is an n-bit pattern; used for signed representation and subtraction. Unsigned n-bit range: 0 … 2^n − 1 — n is number of bits (unitless); applies to pure binary magnitude. 2’s complement n-bit range: −2^(n−1) … 2^(n−1) − 1 — Applies to signed binary in 2’s complement. LSB weight in base r with k fractional digits = r^(−k) — Resolution of the least significant fractional digit. Hamming distance d_min — Definition: minimum number of differing bit positions between any two code words (unitless). Error detect/correct: detect ≤ d_min − 1, correct ≤ floor((d_min − 1)/2) — Applies to block codes. Parity count for Hamming code: find m such that 2^m ≥ m + k + 1 — k is data bits, m parity bits, total n = m + k; this ensures single-error correction (minimum distance 3). To also guarantee detection of all double-bit errors (SECDED), add one overall parity bit (minimum distance 4). BCD 8421 weights = 8, 4, 2, 1 — Each 4-bit group encodes one decimal digit 0–9. Excess‑3 digit code = BCD(digit) + 0011₂ — Add binary 3 to each decimal digit’s 4-bit BCD. Binary↔Gray: g_{n−1} = b_{n−1}; g_i = b_{i+1} XOR b_i; b_{n−1} = g_{n−1}; b_i = b_{i+1} XOR g_i — Index i decreases from n−2 down to 0.

Worked examples

Example 1: Convert and group across bases Given: Convert 45.6875₁₀ to (a) binary, (b) octal, (c) hexadecimal.

  1. Integer part (45₁₀ → binary):
    • Divide by 2: 45/2 = 22 remainder 1; 22/2 = 11 r 0; 11/2 = 5 r 1; 5/2 = 2 r 1; 2/2 = 1 r 0; 1/2 = 0 r 1.
    • Read remainders upward: 101101₂. Check: 1·32 + 0·16 + 1·8 + 1·4 + 0·2 + 1·1 = 32 + 8 + 4 + 1 = 45.
  2. Fraction part (0.6875₁₀ → binary by repeated ×2):
    • 0.6875×2 = 1.375 → bit 1; frac 0.375.
    • 0.375×2 = 0.75 → bit 0; frac 0.75.
    • 0.75×2 = 1.5 → bit 1; frac 0.5.
    • 0.5×2 = 1.0 → bit 1; frac 0.0 (stop). So fraction = .1011₂.
  3. (a) Binary result: 45.6875₁₀ = 101101.1011₂.
  4. (b) Octal: group binary integer part in 3s from LSB: 101 101 = 5 5; fraction part .101 100? We must group from binary point to right in 3s: .101 100? Wait, our fraction is .1011. Pad to multiple of 3: .101 100? That is incorrect because .1011 → pad with trailing zeros to 6 bits: .101100. Groups: .101 100 → 5 and 4. Therefore 101101.1011₂ = (5 5).(5 4)₈ = 55.54₈. Check with decimal: 55₈ = 5·8 + 5 = 45; .54₈ = 5/8 + 4/64 = 0.625 + 0.0625 = 0.6875.
  5. (c) Hex: group in 4s. Integer: 101101 → pad to 6? For hex we need 4s; pad MSB side to multiple of 4: 0010 1101 → 0x2D (2=0010, D=1101). Fraction: .1011 already 4 bits → B. So 45.6875₁₀ = 2D.B₁₆. Final answers: (a) 101101.1011₂, (b) 55.54₈, (c) 2D.B₁₆.

Example 2: Subtraction by complements Question: Using 10’s complement, compute 527 − 689 in base 10 with n = 3 digits.

  1. Find 10’s complement of 689 with n = 3: 10^3 − 689 = 1000 − 689 = 311.
  2. Add to 527: 527 + 311 = 838.
  3. Check for end carry: 838 < 1000, so no end carry. Result is negative. Take 10’s complement of 838: 1000 − 838 = 162. Attach minus sign. Answer: 527 − 689 = −162. Verification with ordinary subtraction: 689 − 527 = 162, so −162 is correct.

Now binary with 2’s complement. Question: Using 2’s complement on 8 bits, compute 10110110₂ − 01100111₂. Given numbers: A = 10110110₂, B = 01100111₂.

  1. 2’s complement of B: Invert bits 01100111 → 10011000; add 1 → 10011001.
  2. Add A + (2’s comp of B): 10110110 + 10011001 = 1 01001111 (9-bit sum). Discard carry out (leftmost 1). Result 01001111.
  3. Interpret: Check signed overflow by comparing the carries into and out of the MSB (or by operand/result signs); here they are equal, so no overflow. Result is 01001111₂.
  4. Convert to decimal to check: A (as unsigned) 182, B 103; 182 − 103 = 79. 01001111₂ = 64 + 8 + 4 + 2 + 1 = 79. Correct. Answer: 01001111₂.

Example 3: Encoding and error control Part (a): Encode 259₁₀ into (i) 8421 BCD, (ii) Excess‑3, and (iii) ASCII digits.

  • 259 has digits 2, 5, 9. (i) 8421 BCD per digit:
    • 2 → 0010, 5 → 0101, 9 → 1001. So BCD = 0010 0101 1001. (ii) Excess‑3 (add 0011 to each 4-bit BCD digit):
    • 2: 0010 + 0011 = 0101; 5: 0101 + 0011 = 1000; 9: 1001 + 0011 = 1100. XS‑3 = 0101 1000 1100. (iii) ASCII for characters '2','5','9':
    • '2' = 0x32 = 0011 0010; '5' = 0x35 = 0011 0101; '9' = 0x39 = 0011 1001. Part (b): Convert Gray code 1101₂(G) to binary, and 10110₂(binary) to Gray.
  • Gray to binary (g3 g2 g1 g0 = 1 1 0 1): b3 = g3 = 1; b2 = b3 XOR g2 = 1 XOR 1 = 0; b1 = b2 XOR g1 = 0 XOR 0 = 0; b0 = b1 XOR g0 = 0 XOR 1 = 1. So binary = 1001₂.
  • Binary to Gray for 10110₂ (b4…b0 = 1 0 1 1 0): g4 = b4 = 1; g3 = b4 XOR b3 = 1 XOR 0 = 1; g2 = b3 XOR b2 = 0 XOR 1 = 1; g1 = b2 XOR b1 = 1 XOR 1 = 0; g0 = b1 XOR b0 = 1 XOR 0 = 1. So Gray = 11101₂. Part (c): Single-error-correcting Hamming code (7,4) for data bits D = d3 d2 d1 d0 = 1 0 1 1.
  • Positions 1..7: p1 p2 d3 p4 d2 d1 d0.
  • Place data: _ _ 1 _ 0 1 1.
  • Parity sets (even parity): • p1 covers positions with LSB of index = 1 → {1,3,5,7} → bits p1, d3=1, d2=0, d0=1 → sum 1+0+1 = 2 (even) ⇒ p1 = 0. • p2 covers indexes with second bit = 1 → {2,3,6,7} → bits p2, d3=1, d1=1, d0=1 → sum 1+1+1 = 3 (odd) ⇒ p2 must be 1 to make total even. • p4 covers indexes with third bit = 1 → {4,5,6,7} → bits p4, d2=0, d1=1, d0=1 → sum 0+1+1 = 2 (even) ⇒ p4 = 0.
  • Code word: p1 p2 d3 p4 d2 d1 d0 = 0 1 1 0 0 1 1 → 0110011. Now suppose a single-bit error flips bit at position 5 (d2). Received word: 0111011.
  • Syndrome bits (recompute parity): • s1 over {1,3,5,7}: 0,1,1,1 → sum 3 (odd) ⇒ s1 = 1. • s2 over {2,3,6,7}: 1,1,1,1 → sum 4 (even) ⇒ s2 = 0. • s4 over {4,5,6,7}: 0,1,1,1 → sum 3 (odd) ⇒ s4 = 1.
  • Syndrome (s4 s2 s1) = 1 0 1 = 5 ⇒ error at position 5. Correct by flipping bit 5 to 0. Data out: 1 0 1 1.

Common mistakes

  • Forgetting to pad with leading/trailing zeros when grouping bits for octal/hex, causing wrong digit boundaries.
  • Applying signed 2’s complement range to unsigned questions (or vice versa).
  • In 1’s complement subtraction, forgetting the end-around carry addition; in 2’s complement subtraction, incorrectly adding an extra 1 after addition.
  • Using binary point methods incorrectly: repeatedly dividing a fraction instead of multiplying by base; or reading the integer parts upward instead of downward.
  • Confusing Gray code with binary count; writing 0111 → 1000 as a 4-bit Gray transition (that changes 4 bits), which violates the one-bit change rule.
  • Hamming code parity sets misapplied: including wrong positions or using odd parity when the question says even parity (or vice versa).
  • Writing ASCII codes for digits by taking numeric value directly (e.g., '5' as 0101) instead of 0x35.

For your exam

  • Short notes: “Advantages of 2’s complement over 1’s complement,” “Why Gray code is used in encoders,” “Self-complementing codes,” “Weighted vs non-weighted codes,” “Binary fractions and fixed-point resolution.” Include one labelled sketch if asked (e.g., place-value table, Gray sequence).
  • Conversions: Decimal↔binary (integers and fractions); binary↔octal/hex using grouping; decimal↔BCD/Excess‑3; binary↔Gray. Show step-by-step workings and a check-back to decimal when time permits.
  • Arithmetic: Addition/subtraction using r’s complement and (r−1)’s complement; detect overflow; show carries and the final decision about sign/end-carry.
  • Coding and errors: Form a (7,4) Hamming code for given data; locate and correct a single-bit error; compute Hamming distance; parity bit usage and limitations. Show parity coverage sets and syndrome calculation.
  • Diagrams: If asked to draw, make clean tables/bit-grouping diagrams, with all labels (weights r^i, bit positions, parity coverage) and neat grouping boxes. A full-marks answer is organised, uses correct symbols and base subscripts (e.g., 1011₂, 3A₁₆), shows intermediate steps (remainders, carries, XORs), and includes a brief verification.

Quick check

  1. What is the 2’s complement 8-bit representation of −37₁₀?
  2. Convert Gray code 1000₂(G) to binary.
  3. Encode the decimal number 90 in 8421 BCD and in Excess‑3. Answers: 1) +37 = 0010 0101 → invert 1101 1010 → +1 = 1101 1011, so −37 = 11011011₂. 2) b3 = 1; b2 = 1 XOR 0 = 1; b1 = 1 XOR 0 = 1; b0 = 1 XOR 0 = 1 → 1111₂. 3) BCD: 9→1001, 0→0000 → 1001 0000; Excess‑3: 9→1100, 0→0011 → 1100 0011.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?