L
LLLOS.ai
Learn
L

Chapter 2 — Data Representation

Class 11 · Computer Science

Overview

Chapter 2 — Data Representation Master Diagram

Introduction: This chapter explains how computers store and manipulate all kinds of information — numbers, text, images and sound — using binary digits (bits). It introduces number systems, methods for converting between bases, binary arithmetic and ways to represent positive and negative integers. The chapter also covers character codes (ASCII, Unicode), common binary codes (BCD, Gray), basic ideas of fixed- and floating-point representation, and simple techniques used to represent images and audio digitally. Importance: Data representation is a foundational topic for understanding how software and hardware interact. Knowledge of how data is encoded and stored helps students write correct programs, estimate storage needs, debug low-level problems, and understand file formats, networking and system limits (overflow, precision loss). It builds essential reasoning skills used throughout computer science and programming. Key themes: number systems (binary, octal, decimal, hexadecimal), conversions and arithmetic in binary; representations for signed integers (signed magnitude, one's complement, two's complement) and range/overflow; character and numeric codes (ASCII, Unicode, BCD,…

Learning Objectives

  • Define binary, octal, decimal and hexadecimal number systems and explain the place-value significance of each digit
  • Convert numbers between binary, octal, decimal and hexadecimal systems accurately
  • Perform binary addition, subtraction, multiplication and division and identify overflow conditions
  • Explain methods for representing signed integers: sign-magnitude, one's complement and two's complement
  • Apply two's complement representation to express negative integers and carry out signed binary arithmetic
  • Explain representation of fractional numbers and convert between decimal fractions and binary fractions
  • Describe the basic IEEE 754 floating-point format (single precision) and interpret sign, exponent and mantissa fields
  • Convert simple decimal numbers to IEEE 754 single-precision representation and convert back to decimal

Topics in this chapter

13 topics · tap a topic title to jump straight to it.

📊1

Data Units

💻 COMPUTER SCIENCE / IT

Data Units

Key Point: 1 byte = 8 bits

What are Data Units?

Data units are standard quantities used to measure and express digital information. The smallest unit is the bit (binary digit), which can be 0 or 1. Eight bits make one byte, the basic addressable storage unit in most computers. Larger units are formed by grouping bytes.

Common units (binary vs decimal)

There are two common ways to define larger units:

  • Binary (used in computing, powers of 2): 1 KB (commonly) = 1024 bytes = 2^10 B. Official binary prefixes are KiB, MiB, GiB, etc. Example sequence: 1 KiB = 1024 B; 1 MiB = 1024 KiB = 1,048,576 B; 1 GiB = 1024 MiB = 1,073,741,824 B.
  • Decimal (used by storage manufacturers, powers of 10): 1 kB = 1000 bytes; 1 MB = 1,000,000 bytes; 1 GB = 1,000,000,000 bytes.

Basic definitions

  • Bit (b): smallest unit, value 0 or 1.
  • Nibble: 4 bits.
  • Byte (B): 8 bits.
  • Kilobyte (KB) ~ 10^3 bytes (decimal) or Kibibyte (KiB) = 2^10 = 1024 bytes (binary).
  • Megabyte (MB) / Mebibyte (MiB), Gigabyte (GB) / Gibibyte (GiB), Terabyte (TB) / Tebibyte (TiB), and so on.

Why two systems? Decimal prefixes are simple for marketing (hard disk makers) but operating systems and memory are often based on powers of two, so values shown by OS may differ from drive labels.

Practical considerations

  • Storage capacity (HDD/SSD/RAM) is expressed in bytes (KB/MB/GB/TB).
  • Network speed is expressed in bits per second (bps, kbps, Mbps). To convert to bytes/sec, divide by 8.
  • File sizes, image sizes, and media durations can be calculated from resolution, color depth, sample rate, etc. (see formulas below).

Quick conversion table (binary)

1 Byte= 8 bits
1 KiB= 1024 B
1 MiB= 1024 KiB = 1,048,576 B
1 GiB= 1024 MiB ≈ 1.074 × 10^9 B
1 TiB= 1024 GiB ≈ 1.100 × 10^12 B

Note on notation: Use capital B for bytes and lowercase b for bits (e.g., MB vs Mbps).

📌 Examples
  • Text file: 500 ASCII characters → 500 bytes ≈ 0.49 KiB (500 / 1024).
  • Image (uncompressed): 5 megapixels, 24-bit color → size = 5,000,000 pixels × 24 bits / 8 = 15,000,000 bytes ≈ 14.31 MiB.
  • Audio (CD quality): 44.1 kHz sample rate, 16-bit depth, stereo (2 channels), 60 seconds → bits = 44100 × 16 × 2 × 60 = 1,411,200 × 60 = 84,672,000 bits → bytes = 84,672,000 / 8 = 10,584,000 bytes ≈ 10.09 MiB.
  • Video (rough uncompressed 1080p frame): 1920 × 1080 pixels, 24-bit color, 30 fps, 10 seconds → bits per frame = 1920×1080×24; total bits = bits_per_frame × 30 × 10; huge—so video is always compressed for practical use.
  • Network: 8 Mbps download → 8 megabits/sec = 8,000,000 bits/sec ≈ 1,000,000 bytes/sec ≈ 0.95 MiB/s (binary).
🧮 Formulas
  1. \[1 byte = 8 bits\]
  2. \[size_in_unit = size_in_bytes / (base^n)\]
    \[where base = 1024 (binary) or 1000 (decimal) and n = 1 for KB, 2 for MB\]
    \[etc.\]
  3. \[bits_needed = ceil(log2(N)) for representing N distinct values\]
  4. \[text_size_bytes = number_of_characters × bits_per_character / 8 (e.g.\]
    \[ASCII uses 7–8 bits\]
    \[UTF-8 is variable)\]
  5. \[image_size_bytes (uncompressed) = width × height × color_depth_in_bits / 8\]
  6. \[audio_size_bytes = sample_rate × bit_depth × channels × duration_seconds / 8\]
🔢2

Number Systems

💻 COMPUTER SCIENCE / IT

Number Systems

Key Point: Positional value: N = Σ (d_i × b^i), where d_i is digit at position i and b is the base.

What are Number Systems? Number systems are ways to represent quantities using symbols (digits) and a base (radix). In computing, number systems let machines store and manipulate numbers in binary (base 2), octal (base 8), decimal (base 10) and hexadecimal (base 16).

Positional notation: In a base b system, a number's value is the sum of each digit multiplied by the base raised to the digit's position power (positions counted from right, starting at 0). For example: digits d_n...d_2 d_1 d_0 represents value = Σ (d_i × b^i).

Common bases:

  • Binary (base 2): digits 0,1. Used internally by computers (bits).
  • Octal (base 8): digits 0–7. Useful historically for compact binary grouping (3 bits per octal digit).
  • Decimal (base 10): digits 0–9. Human everyday use.
  • Hexadecimal (base 16): digits 0–9 and A–F. Compact form for binary (4 bits per hex digit), widely used in programming and color codes.

Conversion methods:

  • Integer to another base: repeated division by the target base, collect remainders (least significant digit first).
  • Fraction to another base: repeated multiplication of fractional part by the base, collect integer parts (most significant digit first).
  • Shortcuts: group binary bits in triples (for octal) or nibbles of 4 (for hex) to convert quickly between binary and those bases.

Signed numbers and complements: Computers represent negative integers using schemes like sign-magnitude, 1's complement, and most commonly 2's complement. In n-bit 2's complement, a negative integer −x is stored as 2^n − x. This simplifies arithmetic and has a numeric range asymmetry (one more negative value than positive).

Other representations: BCD (Binary-Coded Decimal) stores each decimal digit in 4 bits; floating point represents real numbers with sign, exponent and mantissa (not typically part of basic number systems but important for real-number representation).

Why it matters: Understanding number systems is essential for binary arithmetic, memory addressing, data encoding (e.g., colors in hex), debugging, and understanding limits like overflow in fixed-bit hardware.

📌 Examples
  • Convert decimal 45 to binary: 45 ÷ 2 = 22 R1; 22 ÷ 2 = 11 R0; 11 ÷ 2 = 5 R1; 5 ÷ 2 = 2 R1; 2 ÷ 2 = 1 R0; 1 ÷ 2 = 0 R1 → read remainders bottom-up: 101101₂.
  • Convert binary 1101.101₂ to decimal: integer part 1×2^3+1×2^2+0×2^1+1×2^0 = 8+4+0+1 = 13. Fraction part 1×2^-1 + 0×2^-2 + 1×2^-3 = 0.5 + 0 + 0.125 = 0.625. Total = 13.625₁₀.
  • Hex to decimal: 2A₁₆ = 2×16 + 10 = 42₁₀. (Hex digit A = 10.)
  • 8-bit two's complement example: represent −5. +5 = 00000101₂ → invert bits: 11111010 → add 1: 11111011₂. So −5 is 11111011₂ in 8-bit two's complement.
  • Overflow example (8-bit unsigned): 200 + 100 = 300. But 8-bit unsigned range is 0–255, so stored result = 300 − 256 = 44 with a carry out (overflow).
🧮 Formulas
  1. \[Positional value: N = Σ (d_i × b^i)\]
    \[where d_i is digit at position i and b is the base.\]
  2. \[Unsigned n-bit range: 0 to 2^n − 1.\]
  3. \[Signed n-bit two's complement range: −2^(n−1) to 2^(n−1) − 1.\]
  4. \[Two's complement of x (n bits): representation = 2^n − x (for x > 0)\]
    \[Equivalently: invert bits of x and add 1.\]
  5. \[Integer to base-b conversion: repeatedly divide by b\]
    \[remainders give digits (LSB → MSB).\]
  6. \[Fraction to base-b conversion: repeatedly multiply fractional part by b\]
    \[integer parts form digits (MSB → LSB).\]
🧪3

Base Conversions

⚗️ CHEMICAL PRINCIPLE

Base Conversions

Key Point: Positional notation: N = Σ (d_i × b^i) for i ranging over integer and negative indices for fractional digits.

What are base conversions?

Base conversion is the process of changing a number from one positional numeral system (base) to another. A positional system with base b represents a number N as a sum of digits times powers of b:

N = d_n b^n + d_{n-1} b^{n-1} + ... + d_1 b + d_0 + d_{-1} b^{-1} + d_{-2} b^{-2} + ...

Each digit d_i is an integer in the range 0 to b-1. Common bases are decimal (base 10), binary (base 2), octal (base 8) and hexadecimal (base 16).

Why it matters (real-life relevance)

  • Computers use binary internally; programmers often use hexadecimal for compact representation of binary data (memory addresses, color codes like #FF5733).
  • Networking uses binary for IP addresses and masks; conversions help compute subnets.
  • Digital electronics and embedded systems design require base conversions for bit-level operations.

General methods

1. Integer part (decimal or any base) to another base — repeated division method

Divide the integer by the new base repeatedly, record remainders. The converted number's digits (least significant to most) are the remainders read in reverse (last remainder is most significant).

2. Fractional part conversion — repeated multiplication method

Multiply the fractional part by the new base, take the integer part of the result as the next digit, then continue with the new fractional part. Repeat until fractional part becomes 0 or desired precision achieved.

3. Converting via decimal or directly between non-decimal bases

You can convert from any base A to base B by first converting A to decimal (base 10) using positional expansion, then from decimal to B using the division/multiplication methods. For many common pairs there's a direct shortcut:

  • Binary ↔ Hexadecimal: group binary digits in blocks of 4 (2^4 = 16), map each 4-bit group to one hex digit.
  • Binary ↔ Octal: group binary digits in blocks of 3 (2^3 = 8), map each 3-bit group to one octal digit.

Accuracy and repeating fractions

Some fractions in one base are repeating in another (e.g., 1/3 in decimal is 0.333..., and in binary it is 0.010101...). When converting fractions you may need to round or truncate to a fixed number of digits.

📌 Examples
  • Example 1 — Decimal integer to Binary (156 to base 2): 156 ÷ 2 = 78 R0; 78 ÷ 2 = 39 R0; 39 ÷ 2 = 19 R1; 19 ÷ 2 = 9 R1; 9 ÷ 2 = 4 R1; 4 ÷ 2 = 2 R0; 2 ÷ 2 = 1 R0; 1 ÷ 2 = 0 R1. Read remainders bottom-up: 10011100₂.
  • Example 2 — Binary fraction to Decimal (1101.101₂): Integer part 1101₂ = 1·2^3+1·2^2+0·2^1+1·2^0 = 8+4+0+1 = 13. Fractional part .101₂ = 1·2^{-1}+0·2^{-2}+1·2^{-3} = 0.5+0+0.125 = 0.625. Combined = 13.625₁₀.
  • Example 3 — Decimal fraction to Binary (0.375 to base 2): 0.375×2=0.75 → digit 0; 0.75×2=1.5 → digit 1 (fraction 0.5); 0.5×2=1.0 → digit 1 (fraction 0). Result 0.011₂ = 0.375₁₀.
  • Example 4 — Hexadecimal to Binary (A F 3₁₆): A → 1010, F → 1111, 3 → 0011. Combine: 101011110011₂. (Group into nibbles for compact conversion.)
  • Example 5 — Binary to Octal (111101₂ to base 8): Group bits in 3s from right: 111 101 → 7 5, so 75₈ = 111101₂.
🧮 Formulas
  1. \[Positional notation: N = Σ (d_i × b^i) for i ranging over integer and negative indices for fractional digits.\]
  2. \[Integer conversion (to base b): repeatedly divide by b\]
    \[remainders r_k are digits\]
    \[N_baseb = r_n ... r_2 r_1 r_0 (remainders read in reverse).\]
  3. \[Fraction conversion (to base b): multiply fractional part f by b\]
    \[next digit = floor(b×f)\]
    \[new fractional part = frac(b×f)\]
    \[repeat.\]
  4. \[Digit range: for base b\]
    \[each digit d satisfies 0 ≤ d ≤ b−1\]
    \[For hex\]
    \[digits 10–15 represented as A–F.\]
  5. \[Grouping shortcuts: binary ↔ octal: group binary bits in 3s (because 2^3=8). binary ↔ hex: group binary bits in 4s (because 2^4=16).\]
💻4

Binary Arithmetic

💻 COMPUTER SCIENCE / IT

Binary Arithmetic

Key Point: Positional value: value = Σ_{i=0 to n-1} b_i × 2^i

What is Binary Arithmetic?
Binary arithmetic is arithmetic performed using the binary number system (base 2), where each digit (bit) is 0 or 1. Computers perform all numeric operations in binary using logical circuits (adders, multipliers, etc.).

Positional value: each bit in a binary number represents a power of two: from right to left bits represent 2^0, 2^1, 2^2, ... For an n-bit unsigned number b_{n-1}...b_1 b_0 the value is Σ b_i·2^i.

Basic rules for binary addition (bitwise):

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 0 with carry 1
  • 1 + 1 + carry(1) = 1 with carry 1
Binary addition is done right-to-left, propagating carries.

Binary subtraction can be done two ways:

  • Direct borrow method (like decimal subtraction): subtract bit by bit, borrow when needed.
  • Using complements (recommended in digital systems): convert the subtrahend to its two's complement and add. Two's complement of an n-bit number X is (~X) + 1 (bitwise invert then add 1). This turns subtraction A - B into A + (two's complement of B). If there's a carry out beyond n bits, discard it (indicates positive result); if not, the result is negative in two's complement form.

Binary multiplication uses repeated shifting and addition: multiply by 0 or 1 produces 0 or the multiplicand; shift the partial products left according to position and add them (like long multiplication in decimal).

Binary division is repeated subtraction/shift (long division): align divisor with most significant bits of dividend, subtract if possible, place 1 in quotient, shift divisor right and repeat.

Signed numbers and range (two's complement representation): with n bits the representable signed integer range is -2^{n-1} to 2^{n-1}-1. Two's complement simplifies hardware because addition circuitry can be used for subtraction.

Overflow: occurs when result exceeds representable range. For unsigned addition, overflow is signalled by carry out of the most significant bit. For two's complement signed addition, overflow occurs if adding two numbers of the same sign produces a result with opposite sign (equivalently, carry into sign bit ≠ carry out of sign bit).

Why it matters (real world): Binary arithmetic is the foundation of all computer calculations, digital signal processing, cryptography, networking (checksums), and embedded systems. The ALU (arithmetic logic unit) in CPUs implements these operations in hardware.

📌 Examples
  • Binary addition (example): 1011 (11) + 1101 (13) Stepwise: 1+1=0 carry1; 1+0+carry1=0 carry1; 0+1+carry1=0 carry1; 1+1+carry1=1 carry1 → result 11000 (24).
  • Binary subtraction using two's complement: 10110 (22) - 01101 (13) Two's complement of 01101 → invert → 10010, add 1 → 10011. Now add to 10110: 10110 + 10011 =101001 → discard carry beyond 5 bits → 01001 (9).
  • Binary multiplication: 101 (5) × 11 (3) Partial products: 101 × 1 = 101; shifted left one position for next 1 → 1010; add: 101 + 1010 = 1111 (15).
  • Binary division (long division): 11010 (26) ÷ 11 (3) Align divisor: 11 fits in first two bits 11 → quotient bit 1, subtract → remainder, shift next bit... Final quotient = 1000 (8) remainder = 10 (2).
🧮 Formulas
  1. \[Positional value: value = Σ_{i=0 to n-1} b_i × 2^i\]
  2. \[Bit addition rules: 0+0=0\]
    \[0+1=1\]
    \[1+0=1\]
    \[1+1=0 (carry 1)\]
    \[1+1+1=1 (carry 1)\]
  3. \[Two's complement (n bits): -X ≡ 2^n - X\]
    \[compute by inverting bits then adding 1\]
  4. \[Signed range (n bits\]
    \[two's complement): -2^{n-1} to 2^{n-1} - 1\]
  5. \[Overflow detection (two's complement): overflow if (carry into MSB) ≠ (carry out of MSB)\]
    \[or if adding two positives gives negative\]
    \[or two negatives gives positive\]
💻5

Complements

💻 COMPUTER SCIENCE / IT

Complements

Key Point: Decimal: 9's complement of N (n digits) = (10^n − 1) − N

Definition
A complement of a number is another number which when added to the original yields a fixed base-dependent value (all 9s for decimal 9's-complement, all 1s for binary 1's-complement). Complements are used to simplify subtraction and to represent negative numbers in digital systems.

Types

  • Decimal complements
    • 9's complement of an n-digit decimal number N = (10n - 1) − N
    • 10's complement of N = 10n − N (equivalently 9's complement + 1)
  • Binary complements
    • 1's complement of an n-bit binary number B = (2n − 1) − B — obtained by flipping every bit (0↔1)
    • 2's complement of B = 2n − B — obtained by taking 1's complement and then adding 1 (or by computing bitwise NOT then +1)

Why they matter
Two's complement is the standard representation for signed integers in most computers: it allows a single zero, simple addition/subtraction using the same adder circuitry, and easy detection of overflow. One's complement and decimal complements are useful in some manual subtraction methods and older systems.

How to compute (short methods)

  • 9's complement (decimal): replace each digit d by (9 − d).
  • 10's complement (decimal): take 9's complement then add 1.
  • 1's complement (binary): flip every bit (0→1, 1→0).
  • 2's complement (binary): flip bits then add 1; equivalently compute bitwise NOT and add 1 (i.e., −B ≡ (~B) + 1 mod 2n).

Properties

  • In n bits, 1's-complement representation has two zeros (+0 and −0); 2's-complement has a single zero.
  • Range for n-bit two's complement: −2n−1 ... 2n−1 − 1.
  • Range for n-bit one's complement: −(2n−1 − 1) ... 2n−1 − 1 (and two zeros).
  • Subtraction: A − B can be done as A + (complement of B). In two's-complement arithmetic use 2's complement of B and, if a carry out appears, discard it; if no carry, result is already correct in two's-complement form (may require sign interpretation).

Overflow
In two's-complement addition, overflow occurs when adding two numbers of the same sign produces a result of the opposite sign. Another hardware test: overflow = carry into sign bit XOR carry out of sign bit.

Typical use-cases (real life)

  • CPU integer arithmetic: two's complement used to store negative integers and perform subtraction via addition circuits.
  • Sensors and embedded systems reporting negative values (temperatures, altitudes) use two's complement encoded integers.
  • Manual subtraction on paper can be simplified using decimal complements (9's/10's complement method).

📌 Examples
  • Decimal 9's and 10's complement (n = 3 digits): N = 237. 9's complement = (999 − 237) = 762. 10's complement = 9's complement + 1 = 762 + 1 = 763.
  • Binary 1's and 2's complements (n = 4 bits): B = 0101 (5). 1's complement = 1010. 2's complement = 1010 + 1 = 1011. In 4-bit two's complement, 1011 represents −5 (since 2's complement of 1011 = 0101 = +5).
  • Subtraction using 2's complement (4-bit): compute 7 − 5. +7 = 0111. +5 = 0101. 2's complement of 5 = invert 0101 → 1010, add 1 → 1011. Now 0111 + 1011 = 1 0010. Discard carry out (leftmost 1), result = 0010 = 2.
  • Range example for n = 4 bits: two's complement range = −8 to +7. One's complement range = −7 to +7 (note two distinct zeros in one's complement).
  • Method shortcut to negate a binary number: to get −X in two's complement, compute bitwise NOT(X) then add 1 (useful in ALU design).
🧮 Formulas
  1. \[Decimal: 9's complement of N (n digits) = (10^n − 1) − N\]
  2. \[Decimal: 10's complement of N (n digits) = 10^n − N = (9's complement of N) + 1\]
  3. \[Binary: 1's complement of B (n bits) = (2^n − 1) − B = bitwise NOT(B)\]
  4. \[Binary: 2's complement of B (n bits) = 2^n − B = (1's complement of B) + 1 = (~B) + 1\]
  5. \[Two's-complement range for n bits: −2^(n−1) to 2^(n−1) − 1\]
  6. \[Overflow test (binary add): overflow = carry_into_sign_bit XOR carry_out_of_sign_bit\]
🔢6

Representation of Signed Numbers

💻 COMPUTER SCIENCE / IT

Representation of Signed Numbers

Key Point: Sign‑Magnitude value (n bits): value = (-1)^{s} × magnitude, where s is MSB and magnitude uses remaining n-1 bits.

What it is: Computers store signed (positive and negative) integers in binary. Because binary digits (bits) have no inherent sign, several conventions are used to represent sign and magnitude. The commonly taught methods are Sign‑Magnitude, 1's Complement, 2's Complement and Excess (Bias) notation.

1. Sign‑Magnitude

  • Format: MSB = sign (0 = +, 1 = -), remaining (n-1) bits = magnitude.
  • Value: (-1)^{sign} × magnitude.
  • Range (n bits): -(2^{n-1}-1) to +(2^{n-1}-1). There are two zeros: +0 and -0.
  • Pros/Cons: Simple concept; but arithmetic is awkward because sign must be handled separately.

2. 1's Complement

  • Negative of a number: bitwise invert (complement) the positive representation.
  • Range (n bits): same numeric range as sign‑magnitude: -(2^{n-1}-1) to +(2^{n-1}-1). Two zeros exist (+0 and -0).
  • Addition: perform bitwise addition and, if an end carry out of the MSB occurs, add that carry back into the LSB (end‑around carry).
  • Pros/Cons: Simpler than sign‑magnitude for some operations, but still has two zeros and awkward carries.

3. 2's Complement (most used)

  • Negative of a number: invert all bits of the positive representation, then add 1.
  • Value interpretation: if MSB = 0, value = normal unsigned sum. If MSB = 1, value = -2^{n-1} + sum(of remaining weighted bits).
  • Range (n bits): -2^{n-1} to +2^{n-1}-1. Unique zero (no +0/-0).
  • Arithmetic: addition/subtraction works with the same binary adder used for unsigned numbers. Overflow detection: when adding two numbers, overflow occurs if the carry into the sign bit differs from the carry out of the sign bit; equivalently, if two operands have the same sign but the result has a different sign.
  • Pros/Cons: Eliminates two-zero problem and simplifies arithmetic; widely used in CPUs.

4. Excess (Bias) Notation

  • Stored value = true value + K, where K (bias) is usually 2^{n-1}-1 for n-bit fields used in exponents (e.g., IEEE floating point exponent bias).
  • Allows ordering by unsigned magnitude and is useful for storing exponents.

Key ideas to remember

  • 2's complement is the standard for integer arithmetic in modern processors.
  • Range differences: two's complement allows one extra negative value compared with sign‑magnitude/1's complement because it has a single zero.
  • Overflow detection rules differ by representation; for two's complement use the sign rules described above.
📌 Examples
  • Convert -6 to 4-bit representations: - Positive 6 (4-bit) = 0110 - Sign‑Magnitude: set sign=1 => 1110 - 1's Complement: invert 0110 => 1001 - 2's Complement: invert 0110 => 1001, add 1 => 1010 (which equals -8 + 2 = -6) (Checks: 1110 (sign‑mag) means sign bit 1, magnitude 110 = 6; 1001 (1's comp) and 1010 (2's comp) are negative encodings.)
  • Addition in 4-bit two's complement: 5 + (-3) - 5 = 0101 - +3 = 0011 → -3 (2's comp): invert 1100, add 1 → 1101 - Sum: 0101 + 1101 = 10010 → drop carry (mod 2^4) => 0010 = 2 (correct). No overflow because signs differed.
  • Overflow example (4-bit two's complement): 7 + 3 - 7 = 0111, 3 = 0011; sum = 1010 (interpreted as -6). Two positives produced a negative → overflow occurred. (Range for 4-bit is -8..+7.)
  • Excess/bias example (3-bit field, bias K = 3): stored 3-bit value 100 represents true exponent 4-3 = 1; stored 011 represents 3-3 = 0.
🧮 Formulas
  1. \[Sign‑Magnitude value (n bits): value = (-1)^{s} × magnitude\]
    \[where s is MSB and magnitude uses remaining n-1 bits.\]
  2. \[1's Complement negative: negative(x) = bitwise_not(x_positive).\]
  3. \[2's Complement negative: negative(x) = bitwise_not(x_positive) + 1 (mod 2^n).\]
  4. \[Two's complement conversion to decimal (n bits): if MSB=0 → value = sum_{i=0}^{n-1} b_i 2^i\]
    \[if MSB=1 → value = -2^{n-1} + sum_{i=0}^{n-2} b_i 2^i.\]
  5. \[Range (n bits): - Sign‑Magnitude & 1's Complement: -(2^{n-1}-1) to +(2^{n-1}-1) (two zeros) - Two's Complement: -2^{n-1} to +2^{n-1}-1 (single zero).\]
  6. \[Overflow detection (two's complement addition): overflow if carry_into_MSB != carry_out_of_MSB\]
    \[equivalently\]
    \[overflow occurs when adding two numbers with same sign yields a result with different sign.\]
💻7

Overflow and Underflow

💻 COMPUTER SCIENCE / IT

Overflow and Underflow

Key Point: Unsigned n-bit integer range: 0 to 2^n − 1

Overflow and Underflow describe situations where a computation produces a numeric result that cannot be represented within the fixed number of bits reserved for that number in a computer.

Integer overflow: With fixed-width integer representations (n bits), only a limited set of integers can be represented. For unsigned n-bit integers the representable range is 0 to 2n−1. For signed integers using two's complement the range is −2n−1 to 2n−1−1. If a calculation produces a result outside the allowed range, integer overflow occurs. Behavior depends on the system: arithmetic may wrap-around (modular arithmetic), set a hardware flag, or trigger an exception.

Integer underflow: Commonly used to mean the same wrap-around behavior when a subtraction or negative result falls below the minimum representable value. In unsigned arithmetic subtracting a larger number from a smaller one produces underflow that typically wraps to a large positive value (e.g., 0 − 1 = 2n−1).

Signed overflow detection: When adding two numbers of the same sign yields a result of the opposite sign, a signed overflow has occurred. Unsigned overflow can be detected by a carry out from the most significant bit.

Floating-point overflow and underflow: Floating-point numbers (e.g., IEEE formats) represent values roughly as ±mantissa × 2exponent. Floating-point overflow occurs when a result's exponent is greater than the maximum exponent: the result is typically set to ±infinity. Floating-point underflow occurs when a nonzero result is closer to zero than the smallest positive representable normal number. Behaviors include gradual underflow to subnormal (denormal) numbers with reduced precision or flushing to zero for very small values.

Consequences and handling: Overflow/underflow can produce wrong results, wrap-around effects, or runtime errors. Common mitigations: use larger data types (more bits), check for overflow before performing operations, use saturating arithmetic (clamp to min/max), or use arbitrary-precision libraries for integers and high-precision or scaled arithmetic for reals.

Summary: Overflow and underflow are fundamental limits caused by finite representation. Integer cases produce wrap-around or flagged errors; floating-point cases produce infinities, zeros, or reduced-precision subnormal numbers.

📌 Examples
  • 8-bit unsigned overflow: 11111111 (255) + 1 = 1 00000000 → stored as 00000000 (0) with carry-out = 1 (wrap-around).
  • 8-bit signed two's complement overflow: 01111111 (127) + 00000001 (1) = 10000000 which is −128 in two's complement → signed overflow (result sign changed).
  • Unsigned underflow (subtraction): 8-bit unsigned 00000000 (0) − 00000001 (1) → underflow, result stored as 11111111 (255) (wrap-around).
  • Floating-point overflow (conceptual): If the floating format can represent up to about 1×10^38 and a computation produces 1×10^40, the value becomes +infinity (or triggers an overflow error).
  • Floating-point underflow (conceptual): If the smallest positive normal value is ≈1×10^−38, a result of 1×10^−50 may become a subnormal number with reduced precision or be flushed to 0.
🧮 Formulas
  1. \[Unsigned n-bit integer range: 0 to 2^n − 1\]
  2. \[Signed n-bit two's complement range: −2^(n−1) to 2^(n−1) − 1\]
  3. \[Unsigned overflow test (addition): carry_out_from_MSB = 1 indicates overflow (wrap-around occurred)\]
  4. \[Signed overflow test (addition): if sign(a) = sign(b) and sign(result) ≠ sign(a)\]
    \[then signed overflow occurred\]
  5. \[Smallest positive normal floating-point (IEEE-like): +2^(1−bias) (dependent on exponent width and bias)\]
    \[values smaller may become subnormal or 0\]
  6. \[Floating-point dynamic range ≈ ±2^(emin) to ±2^(emax) where emin and emax depend on exponent bits\]
🛟8

Fixed-point and Floating-point Representation

💻 COMPUTER SCIENCE / IT

Fixed-point and Floating-point Representation

Key Point: Fixed-point stored integer N represents value V = N / 2^f (f = number of fractional bits).

Overview

Computers store real numbers (numbers with fractional parts) using two common schemes: fixed-point and floating-point. Both represent real values in binary, but they trade off range, precision, complexity and hardware cost.

Fixed-point representation

In fixed-point representation the binary point (radix point) is at a fixed position for all numbers. A fixed number of bits is reserved for the integer part and a fixed number for the fractional part. A fixed scaling factor 2^(-f) (where f is the number of fractional bits) is implied.

  • Representation: store an integer N; interpreted value = N / 2^f.
  • Notation: Qm.n (or Q format) means m integer bits and n fractional bits (excluding sign bit if signed).
  • Advantages: simple, fast, deterministic precision; good for embedded systems, digital signal processing, financial apps.
  • Limitations: limited dynamic range; uniform spacing between representable values (fixed precision across the whole range).

Example (8-bit unsigned with 4 fractional bits): to represent 6.75, compute 6.75 × 2^4 = 108 (decimal) = 01101100 (binary). Interpreted value = 108 / 16 = 6.75.

Floating-point representation

Floating-point stores numbers in scientific notation (binary): a sign, an exponent, and a significand (mantissa). The binary point "floats" because the exponent scales the significand, giving a much larger dynamic range.

  • Generic value: (-1)^S × (1.fraction) × 2^(E - bias) for normalized numbers (binary normalized form with an implicit leading 1).
  • IEEE 754 is the common standard. Typical formats:
  • Single precision (32-bit): 1 sign bit, 8 exponent bits, 23 fraction bits. Bias = 127.
  • Double precision (64-bit): 1 sign bit, 11 exponent bits, 52 fraction bits. Bias = 1023.

Special encodings exist for zero, subnormal (denormal) numbers, infinity and NaN.

Floating-point properties:

  • Large dynamic range (can represent very large and very small magnitudes).
  • Precision relative to magnitude is roughly constant (relative or percentage error roughly constant), but absolute spacing grows with magnitude.
  • More complex hardware and rounding rules; susceptible to rounding and cancellation errors in arithmetic.

Example (IEEE 754 single): 13.25(decimal) = 1101.01₂ = 1.10101₂ × 2^3. Exponent field = 3 + 127 = 130 = 10000010₂. Fraction bits = 101010000... (pad to 23 bits). Final 32-bit pattern: 0 10000010 10101000000000000000000.

When to use which

  • Fixed-point: use when deterministic precision and low-cost hardware are required (embedded controllers, audio codecs, financial calculations where scaling is fixed).
  • Floating-point: use for scientific computing, graphics, general-purpose programs that need wide dynamic range (physics simulations, machine learning).
📌 Examples
  • Fixed-point: Financial system storing rupees with two decimal places uses integer cents (value = stored_integer / 100). Binary equivalent: with f fractional bits, value = stored_integer / 2^f.
  • Fixed-point (binary) example: 8-bit signed with 4 fraction bits (Q3.4). To represent 6.75: 6.75 × 16 = 108 -> 01101100 (binary stored). Interpreted as 108/16 = 6.75.
  • Floating-point (IEEE 754 single) example: 13.25 -> binary 1101.01₂ -> normalized 1.10101₂ × 2^3 -> sign=0, exponent=3+127=130 (10000010₂), fraction=10101000... -> 0 10000010 10101000000000000000000.
  • Real-life uses: Fixed-point in microcontrollers for motor control and audio DSP; floating-point in graphics rendering, scientific simulations, and machine learning.
  • Edge case: Subnormal (denormal) floating numbers allow representation of values closer to zero than normalized range but with lower precision; infinities and NaNs represent overflow and invalid operations.
🧮 Formulas
  1. \[Fixed-point stored integer N represents value V = N / 2^f (f = number of fractional bits).\]
  2. \[Floating-point value (normalized binary): V = (-1)^S × (1.F) × 2^(E - bias)\]
    \[where S = sign bit\]
    \[F = fractional bits of significand\]
    \[E = stored exponent\]
    \[bias = 2^(k-1) - 1 (k = exponent bits).\]
  3. \[Bias formula: bias = 2^(k-1) - 1 (e.g.\]
    \[k=8 → bias=127\]
    \[k=11 → bias=1023).\]
  4. \[Exponent range for normalized numbers: E_min = 1 - bias\]
    \[E_max = (2^k - 2) - bias. (Stored exponents 0 and 2^k-1 reserved for denormals/zero and Inf/NaN.)\]
  5. \[Machine epsilon (distance between 1 and next representable number): eps = 2^(1-p)\]
    \[where p = precision (number of significand bits including implicit leading 1)\]
    \[For IEEE single p=24 → eps = 2^-23.\]
  6. \[Relative spacing (approx): spacing ≈ 2^(E - p + 1) for numbers with exponent E (shows that spacing grows with magnitude in floating-point).\]
🛟9

IEEE 754 Floating-point Standard

💻 COMPUTER SCIENCE / IT

IEEE 754 Floating-point Standard

Key Point: Normalized value: V = (−1)^S × 1.F × 2^(E − bias)

What it is
IEEE 754 is the most widely used standard for representing real (floating-point) numbers in computers. It defines binary formats (single and double precision), how to encode sign, exponent and fraction (mantissa), special values (zero, infinity, NaN), normalization rules, and rounding methods.

Basic structure
A floating-point number is stored in three fields: sign (S), exponent (E), and fraction (F).

  • Single precision (32-bit): 1 bit sign, 8 bits exponent, 23 bits fraction.
  • Double precision (64-bit): 1 bit sign, 11 bits exponent, 52 bits fraction.

Normalized numbers (most common)
Normalized representation assumes an implicit leading 1 before the binary point (called the hidden bit). The stored exponent is biased so that both positive and negative real exponents can be encoded.

Value formula (normalized)
Value = (-1)S × 1.F × 2(E - bias)
Here 1.F means the binary number formed by placing a binary point after an implicit 1 followed by the fraction bits.

Denormalized (subnormal) numbers
When the stored exponent is all zeros (E = 0), numbers are represented without the implicit leading 1 to allow representation of magnitudes closer to zero. Their value is:

Value = (-1)S × 0.F × 2(1 - bias)

Special cases

  • E = all 1s and F = 0 => ±Infinity (sign bit chooses + or −)
  • E = all 1s and F ≠ 0 => NaN (Not a Number)
  • E = 0 and F = 0 => ±0 (signed zero)

Bias and ranges

  • Single precision bias = 127. Exponent field 1..254 encodes e = E - 127 (so e range for normalized numbers is −126..+127).
  • Double precision bias = 1023. Exponent field 1..2046 encodes e = E - 1023 (so e range −1022..+1023).

Precision and limits (single precision examples)

  • Smallest positive normalized ≈ 2−126 ≈ 1.17549435×10−38
  • Smallest positive denormal ≈ 2−149 ≈ 1.40129846×10−45
  • Largest finite ≈ (2 − 2−23) × 2127 ≈ 3.4028235×1038
  • Machine epsilon (difference between 1 and next representable number) ≈ 2−23 ≈ 1.1920929×10−7 for single precision.

Rounding
IEEE 754 defines rounding modes (round to nearest even is the default), so results of arithmetic are rounded to fit the available precision. This causes small rounding errors that are normal in floating-point arithmetic.

Why normalization and bias?
Normalization maximizes precision by keeping the leading digit nonzero. The bias makes exponent storage unsigned while allowing negative exponents.

Important consequences for students

  • Not all decimal real numbers can be represented exactly in binary floating point (e.g., 0.1).
  • Floating-point arithmetic is fast and wide-ranging in magnitude but not exact; avoid testing equality of floats directly—use tolerances.
  • Understand special values: signed zero, ±infinity, and NaN affect comparisons and computations.

Short conversion recipe (decimal to IEEE 754 single)

  1. Find sign bit S (0 for positive, 1 for negative).
  2. Convert absolute value to binary and normalize to form 1.F × 2e.
  3. Compute biased exponent E = e + 127 and write it in 8 bits.
  4. Take the fractional bits after the binary point (F), fill/truncate to 23 bits using rounding rule.
  5. Concatenate S | E | F to get the 32-bit pattern.

📌 Examples
  • Convert +5.75 to IEEE 754 single precision: 5.75₁₀ = 101.11₂ = 1.0111₂ × 2² => S=0, e=2, E=2+127=129 => E=10000001₂; fraction F=011100... (pad to 23 bits). Final 32-bit: 0 10000001 01110000000000000000000.
  • Convert −0.15625 to IEEE 754 single: 0.15625₁₀ = 0.00101₂ = 1.01₂ × 2^(−3) => S=1, e=−3, E=124 (01111100₂), F=010000... => bits: 1 01111100 01000000000000000000000.
  • Special values in single precision: +0 => 0 00000000 000...0, −0 => 1 00000000 000...0, +∞ => 0 11111111 000...0, NaN => 0 11111111 nonzero-fraction (e.g., quiet NaN often 0 11111111 100...0).
  • Demonstration of rounding effect: 0.1 cannot be represented exactly in binary, so adding 0.1 repeatedly can produce small accumulated errors; use tolerances for comparisons.
🧮 Formulas
  1. \[Normalized value: V = (−1)^S × 1.F × 2^(E − bias)\]
  2. \[Denormalized value: V = (−1)^S × 0.F × 2^(1 − bias)\]
  3. \[Bias (single) = 127\]
    \[Bias (double) = 1023\]
  4. \[Exponent range (single normalized): e ∈ [−126, +127]\]
    \[stored E ∈ [1,254]\]
  5. \[Machine epsilon (single) ≈ 2^(−23) ≈ 1.1920929e−7\]
  6. \[Smallest positive normal (single) = 2^(−126) ≈ 1.17549435e−38\]
    \[smallest positive denormal ≈ 2^(−149) ≈ 1.40129846e−45\]
💻10

Character Representation

💻 COMPUTER SCIENCE / IT

Character Representation

Key Point: Number of distinct values representable with n bits = 2^n

What is Character Representation?
Character representation is the method of mapping human-readable characters (letters, digits, punctuation, symbols) to numeric codes so computers can store and process text. Each character is assigned a code point (a number). That code point is then stored as binary (bits).

Key ideas

  • Code point: an integer assigned to a character (e.g., 'A' = 65).
  • Encoding: how a code point is represented as bytes (examples: ASCII, UTF-8, UTF-16, UTF-32).
  • Glyph vs. Code point: a glyph is the visual shape; many glyphs can map to one code point (or vice versa in complex scripts).

Common encodings

  • ASCII – 7-bit standard for English: 128 code points (0–127). Example: 'A' = 65 (01000001).
  • Extended ASCII – 8-bit (0–255) used historically for extra symbols and some language characters.
  • Unicode – universal standard giving every character a unique code point (U+0000 to U+10FFFF). It covers scripts worldwide, symbols and emoji.
  • UTF-8 – a widely used variable-length encoding for Unicode. Uses 1 byte for ASCII (U+0000–U+007F), 2 bytes for U+0080–U+07FF, 3 bytes for U+0800–U+FFFF, and 4 bytes for U+10000–U+10FFFF.

How a character is stored (example steps)

  1. Choose code point: 'A' → 65 (decimal).
  2. Convert to binary: 65 → 01000001 (8-bit representation).
  3. Store the binary bits in memory (1 byte for this ASCII character).

Why variable-length encodings?
To save space: ASCII characters remain 1 byte (compact for English) while non-Latin characters get more bytes as needed.

Practical notes
Programs must agree on encoding (file headers, HTTP headers, or byte order marks) or text may appear corrupted (mojibake). Modern systems use Unicode (usually UTF-8).

📌 Examples
  • Simple conversion: 'A' → ASCII code 65 → binary 01000001 (1 byte).
  • Digit example: '0' → ASCII 48 → binary 00110000.
  • Lowercase: 'a' → ASCII 97 → binary 01100001; note difference between 'A' and 'a' (case has different codes).
  • Unicode example: 'ह' (Devanagari letter) has code point U+0939; in UTF-8 it is encoded as three bytes: E0 A4 B9 (hex).
  • Emoji example: '😊' has Unicode code point U+1F60A and is encoded in UTF-8 as 4 bytes: F0 9F 98 8A (hex).
  • Storage comparison: the ASCII string "Hello" requires 5 bytes in ASCII/UTF-8, but a string with non-Latin characters may require more bytes per character in UTF-8.
🧮 Formulas
  1. \[Number of distinct values representable with n bits = 2^n\]
  2. \[Minimum bits needed for N symbols = ceil(log2(N))\]
  3. \[Binary to decimal conversion: decimal = Σ (bit_i × 2^i) where i = 0 is LSB\]
  4. \[Byte relation: 1 byte = 8 bits\]
    \[bytes needed = ceil(bits_needed / 8)\]
  5. \[UTF-8 byte-length ranges: 1 byte for U+0000–U+007F, 2 bytes for U+0080–U+07FF, 3 bytes for U+0800–U+FFFF, 4 bytes for U+10000–U+10FFFF\]
  6. \[Storage size (bytes) = Σ (bytes used by each character depending on encoding)\]
💻11

Binary Codes and Encodings

💻 COMPUTER SCIENCE / IT

Binary Codes and Encodings

Key Point: Value of n-bit unsigned binary: V = Σ (b_i × 2^i), i from 0 to n−1.

Overview: Computers store and process data in binary (base-2) using bits (0 or 1). A binary code or encoding is a rule that maps information (numbers, characters, instructions, images) into binary patterns so the computer can store, transmit and interpret it.

Binary number basics: A binary number b_{n-1}...b_1b_0 represents the value Σ (b_i × 2^i). Example: 1101₂ = 1×2^3 + 1×2^2 + 0×2^1 + 1×2^0 = 13.

Numeric encodings (how integers are represented):

  • Unsigned binary: All bits form a positive value. Range for n bits: 0 to 2^n − 1.
  • Sign-magnitude: Most-significant-bit (MSB) is sign (0 = +, 1 = −), remaining bits magnitude. Example (4-bit): 1001 = −1. Two zeros (+0 and −0) exist.
  • One's complement: Negative represented by bitwise inversion of positive. Example (4-bit): +5 = 0101, −5 = 1010. Still has +0 and −0.
  • Two's complement (standard in computers): Negative = invert bits of positive and add 1. Range for n bits: −2^{n−1} to 2^{n−1} − 1. Example (4-bit): +3 = 0011, −3 = 1101.

Other numeric formats:

  • Fixed-point: A fixed number of bits for integer and fractional parts (used in simple arithmetic).
  • Floating-point: IEEE 754 standard uses sign, exponent, mantissa to represent real numbers (scientific notation in binary).

Binary-coded decimal (BCD) and related codes:

  • BCD: Each decimal digit encoded as 4-bit binary (e.g., decimal 27 → 0010 0111). Useful in calculators and financial computations to avoid binary-decimal conversion errors.
  • Excess-3: BCD value + 3 (0011). Helps with some arithmetic and error detection.
  • Gray code: Adjacent numbers differ by exactly one bit (useful in rotary encoders and error-reduction when analog-to-digital transitions occur).

Character encodings:

  • ASCII: 7-bit (commonly stored in 8 bits) encoding for English characters and control codes (128 symbols).
  • Unicode: Universal character set (many languages). Implementations include UTF-8 (variable length, backward compatible with ASCII), UTF-16, UTF-32.

Error detection codes:

  • Parity bit: Add one bit so that total number of 1s is even (even parity) or odd (odd parity). Simple single-bit error detection.
  • Checksums, CRC, Hamming codes: Stronger methods to detect and correct multiple-bit errors (Hamming can correct single-bit errors and detect double-bit errors).

Why different encodings? Each encoding optimizes for ease of arithmetic, compactness, compatibility, error-resilience or human readability. Two's complement simplifies hardware for addition/subtraction; UTF-8 optimizes common-text storage while supporting all Unicode characters; Gray code reduces errors during transitions.

📌 Examples
  • Storing the integer −6 in 8-bit two's complement: positive 6 = 00000110; invert → 11111001; add 1 → 11111010 (represents −6).
  • Representing decimal 59 in BCD: 5 → 0101, 9 → 1001 → 0101 1001.
  • ASCII encoding: character 'A' → 65 decimal → 01000001 in 8-bit ASCII.
  • UTF-8: the Euro sign (€) is U+20AC and encoded as three bytes: 0xE2 0x82 0xAC in UTF-8.
  • Gray code sequence for 3 bits (minimizes bit changes): 000, 001, 011, 010, 110, 111, 101, 100 — used in rotary position sensors to avoid spurious readings.
  • Parity bit example: data 1011001 has three 1s (odd); for even parity add parity bit 1 → 10110011 so total 1s = 4 (even).
🧮 Formulas
  1. \[Value of n-bit unsigned binary: V = Σ (b_i × 2^i)\]
    \[i from 0 to n−1.\]
  2. \[Unsigned range (n bits): 0 to 2^n − 1.\]
  3. \[Two's complement range (n bits): −2^{n−1} to 2^{n−1} − 1.\]
  4. \[Two's complement negation: −x = (bitwise NOT of x) + 1 (mod 2^n).\]
  5. \[Convert binary to decimal: sum of bit × 2^position\]
    \[Convert decimal to binary: repeated division by 2 (collect remainders).\]
  6. \[Parity (even): parity_bit = XOR of all data bits (parity_bit = b_0 ⊕ b_1 ⊕ ... ⊕ b_{n−1}).\]
💻12

Error Detection and Parity

💻 COMPUTER SCIENCE / IT

Error Detection and Parity

Key Point: Even-parity bit: p = (sum of data bits) mod 2. (Set parity bit equal to 1 if the number of 1s in data is odd, so total becomes even.)

What is error detection? When digital data is transmitted or stored, noise or faults can flip bits. Error detection techniques let the receiver detect (and sometimes correct) such errors before using the data.

Parity — basic idea
A parity bit is a single extra bit appended to a group of data bits so that the total number of 1s in the transmitted block follows a chosen rule (even or odd). On reception the receiver recomputes the parity and compares it with the transmitted parity bit. A mismatch indicates an error.

Even and odd parity
- Even parity: parity bit is chosen so that the total count of 1s (data bits + parity bit) is even.
- Odd parity: parity bit is chosen so that the total count of 1s is odd.

How to generate/check parity
Generation: compute the sum (modulo 2) of all data bits and set parity bit accordingly. Checking: recompute the modular sum including the parity bit; if the result violates the chosen parity rule, an error is detected.

Example (conceptual)
Data bits: 1 0 1 1 (three 1s).
- Even parity: parity bit = 1 (so total 1s = 4, even). Transmitted: 1 0 1 1 1.
- If one bit flips during transmission (e.g., third bit 1 -> 0), receiver recomputes parity and finds mismatch – error detected.

What parity detects and its limitations
- Parity detects any odd number of bit errors (1, 3, 5, ... flips) because the total count of 1s changes parity.
- Parity fails to detect an even number of bit errors (2, 4, ...) when flips cancel out the parity change. Thus parity provides simple detection but cannot correct errors and cannot detect all error patterns.

Improvements: block & 2D parity
- Block parity: add a parity bit for each byte/word.
- Two-dimensional (row-and-column) parity: arrange data in a matrix, compute parity for each row and column and send both sets of parity bits. This can detect many burst errors and can locate and correct a single-bit error in the block (by finding the row and column with parity mismatch).

Relation to Hamming distance
The simple parity code has minimum Hamming distance 2. That guarantees detection of any single-bit error but not correction; it will also detect any odd-numbered error patterns.

Where parity is used
Simple parity bits are used in serial interfaces (UART optional parity), some memory and storage schemes historically (parity RAM), and as a component of RAID parity schemes (RAID 5) for disk-failure recovery. Modern networks/files often use stronger checks (CRC, checksums, ECC) when higher reliability is required.

📌 Examples
  • UART serial communication: a byte (e.g., 7 data bits) plus one parity bit (even or odd) is sent so the receiver can detect single-bit errors in the byte.
  • RAID 5 disk array: parity blocks are distributed across disks. If one disk fails, data can be reconstructed using parity information from the remaining disks.
  • Memory parity (historical PCs): each byte stored had an extra parity bit to detect single-bit errors in memory reads.
  • Two-dimensional parity for a 4x4 data block: row and column parity bits allow the system to detect and correct a single flipped bit in the block.
🧮 Formulas
  1. \[Even-parity bit: p = (sum of data bits) mod 2. (Set parity bit equal to 1 if the number of 1s in data is odd\]
    \[so total becomes even.)\]
  2. \[Odd-parity bit: p = 1 XOR (sum of data bits mod 2) = (sum of data bits + 1) mod 2.\]
  3. \[Parity check (even parity): (sum of data bits + parity bit) mod 2 = 0 if no error detected\]
    \[nonzero indicates an error.\]
  4. \[Probability (conceptual) that parity fails to detect random bit flips: P_undetected = sum_{k even\]
    \[k>0} C(n,k) p^k (1-p)^{n-k} for n transmitted bits and single-bit flip probability p (shows even-number flips go undetected).\]
📊13

Data Representation Concepts

💻 COMPUTER SCIENCE / IT

Data Representation Concepts

Key Point: Value of n-bit binary: V = Σ_{i=0}^{n-1} b_i × 2^i

Overview
Data representation is about how computers store and manipulate information (numbers, text, images, sound) using binary digits (bits). A bit is 0 or 1. Groups of bits form bytes (8 bits) and larger units. Understanding representations helps you know ranges, precision, storage size and how arithmetic/logic is done inside a computer.

Basic units and positional value

  • Bit: binary digit (0 or 1). Byte: 8 bits. Common units: KB (2^10 bytes), MB (2^20), GB (2^30), TB (2^40).
  • Positional value (binary): for an n-bit unsigned binary number b_{n-1}...b_1 b_0, its decimal value = Σ b_i * 2^i.

Number systems & conversions
Computers use binary (base 2). Humans often use decimal (base 10), while octal (base 8) and hexadecimal (base 16) are compact notations for binary. Conversion methods include repeated division (decimal→binary) and positional expansion (binary→decimal).

Representing integers
There are several ways to represent signed integers using n bits:

  • Unsigned: range 0 to 2^n - 1.
  • Sign-and-magnitude: highest bit is sign (0 positive, 1 negative); remaining bits give magnitude. Two representations for zero (+0 and -0).
  • One's complement: negative = bitwise NOT of positive. Two zeros exist; arithmetic requires end-around carry.
  • Two's complement: negative = invert bits and add 1. Common in modern CPUs. Range: -2^{n-1} to 2^{n-1}-1. Single zero, simpler arithmetic.

Fixed-point vs Floating-point
Fixed-point stores integers or scaled integers (implied decimal point). Floating-point stores a sign, exponent and mantissa to represent a wide dynamic range using scientific notation in base 2. IEEE 754 is the standard:

  • Single precision (32-bit): 1 sign bit, 8-bit exponent (bias 127), 23-bit fraction (mantissa).
  • Double precision (64-bit): 1 sign bit, 11-bit exponent (bias 1023), 52-bit fraction.

Value formula: (-1)^{sign} × (1.fraction) × 2^{exponent - bias} for normalized numbers.

Character encoding
Characters are mapped to numeric codes: ASCII (7-bit, 0–127) for basic Latin characters; extended ASCII (8-bit); Unicode (code points up to U+10FFFF) with UTF-8/UTF-16/UTF-32 encodings to support all world scripts.

Other representations and codes

  • BCD (Binary Coded Decimal): stores decimal digits separately (each digit in 4 bits). Useful for precise decimal arithmetic in finance.
  • Gray code: consecutive numbers differ by 1 bit; used in analog-to-digital converters and error reduction for mechanical encoders.
  • Parity and checksums: simple error-detection: add a parity bit so total 1s are even (even parity) or odd (odd parity).

Limits, overflow and precision
Every representation has limits. Unsigned overflow occurs when value > 2^n -1. Two's complement overflow happens when result falls outside allowed range. Floating-point has limited precision (rounding) and can represent extremely large/small magnitudes but not all decimals exactly (e.g., 0.1).

Why it matters (real-life impact)
Correct data representation affects storage size, numeric correctness (banking, scientific computing), image/video color depth, network addresses, file formats and performance.

📌 Examples
  • Convert decimal 13 to binary: 13 /2 = 6 r1, 6/2 = 3 r0, 3/2 = 1 r1, 1/2 = 0 r1 → binary 1101.
  • Two's complement of -5 in 8 bits: +5 = 00000101 → invert 11111010 → add 1 → 11111011 (represents -5).
  • ASCII: 'A' = 65 (01000001), 'a' = 97 (01100001).
  • RGB 24-bit color: #FF0000 (255,0,0) is pure red — each channel uses 8 bits so 2^{24} colors total.
  • IEEE 754 single: represent 13.25 → sign 0, exponent 130 (biased), mantissa bits for fractional part → 01000010 10101000 00000000 00000000 (example layout).
  • Storage calculation: a 5-minute mono audio sampled at 44.1 kHz, 16-bit → samples = 5*60*44100 = 13,230,000 samples → bytes = samples * 2 = 26,460,000 bytes ≈ 25.24 MB.
🧮 Formulas
  1. \[Value of n-bit binary: V = Σ_{i=0}^{n-1} b_i × 2^i\]
  2. \[Unsigned range for n bits: 0 to 2^n - 1\]
  3. \[Two's complement range for n bits: -2^{n-1} to 2^{n-1} - 1\]
  4. \[Sign-and-magnitude max magnitude: ±(2^{n-1} - 1)\]
  5. \[Floating-point (normalized): value = (-1)^{sign} × (1.fraction) × 2^{exponent - bias}\]
  6. \[IEEE 754 exponent bias: bias = 2^{k-1} - 1 (k = number of exponent bits)\]

Key Concepts

Bit
Smallest unit of data in computing representing 0 or 1.
Byte
Group of 8 bits used as a basic addressable unit of memory.
Nibble
Group of 4 bits; half a byte.
Binary Number System
Base-2 positional number system using digits 0 and 1.
Decimal Number System
Base-10 positional number system using digits 0–9, used commonly by humans.
Hexadecimal
Base-16 number system using digits 0–9 and A–F; compact for binary grouping (4 bits per hex digit).
Octal
Base-8 number system using digits 0–7 (3 bits per octal digit).
Binary Coded Decimal (BCD)
Representation of each decimal digit by its 4-bit binary equivalent.
ASCII
7/8-bit standard character encoding for English letters, digits and control codes.
Unicode
Universal character encoding standard covering characters from many languages and symbols (e.g., UTF-8, UTF-16).
Binary Arithmetic
Performing arithmetic operations (add, subtract, multiply, divide) using binary numbers and rules of carry/borrow.
Two's Complement
Method to represent signed integers where negative numbers are obtained by inverting bits and adding 1; simplifies arithmetic.
Signed Magnitude
Signed number representation where most significant bit indicates sign (0 positive, 1 negative) and remaining bits give magnitude.
Floating Point Representation
Representation of real numbers in form (sign) × mantissa × base^(exponent), commonly following IEEE-754 standard.
Mantissa
Fractional part (also called significand) of a floating point number after normalization that carries significant digits.
Exponent
Part of floating point that scales the mantissa by a power of the base; usually stored with a bias in IEEE formats.
Normalization
Adjusting a floating point number so that mantissa falls within a fixed range (e.g., leading digit nonzero), maximizing precision.
Overflow
Condition when a calculation produces a value too large to be represented in the available number of bits.
Parity Bit
Single bit added to data to make number of 1s even (even parity) or odd (odd parity) for simple error detection.
Gray Code
Binary encoding where successive values differ by only one bit, used to reduce errors in digital transitions.

Practice Questions

  1. Convert the decimal number 45 to binary, showing the repeated-division steps. / दशमलव संख्या 45 को बाइनरी में बदलिए, बार-बार भाग देने के चरण दिखाते हुए।
    Show answer

    45/2=22 R1, 22/2=11 R0, 11/2=5 R1, 5/2=2 R1, 2/2=1 R0, 1/2=0 R1; reading remainders bottom-up gives 101101. / 45/2=22 शेष1, 22/2=11 शेष0, 11/2=5 शेष1, 5/2=2 शेष1, 2/2=1 शेष0, 1/2=0 शेष1; शेषफलों को नीचे से ऊपर पढ़ने पर 101101 मिलता है।

  2. Represent -5 in 8-bit two's complement form, showing each step. / -5 को 8-बिट टू'ज़ कॉम्प्लीमेंट रूप में दर्शाइए, प्रत्येक चरण दिखाते हुए।
    Show answer

    +5 = 00000101; invert bits to get 11111010; add 1 to get 11111011, which is -5 in 8-bit two's complement. / +5 = 00000101; बिट उलटने पर 11111010; इसमें 1 जोड़ने पर 11111011 मिलता है, जो 8-बिट टू'ज़ कॉम्प्लीमेंट में -5 है।

  3. Why is two's complement preferred over sign-magnitude for integer arithmetic in CPUs? / CPU में पूर्णांक अंकगणित के लिए साइन-मैग्निट्यूड की तुलना में टू'ज़ कॉम्प्लीमेंट को क्यों प्राथमिकता दी जाती है?
    Show answer

    Two's complement has a single representation of zero and lets the same adder circuit perform both addition and subtraction, simplifying hardware, whereas sign-magnitude has two zeros and requires separate handling of the sign. / टू'ज़ कॉम्प्लीमेंट में शून्य का एकमात्र निरूपण होता है और एक ही जोड़ने वाला परिपथ जोड़ व घटाव दोनों कर सकता है, जिससे हार्डवेयर सरल होता है, जबकि साइन-मैग्निट्यूड में दो शून्य होते हैं और चिह्न को अलग से संभालना पड़ता है।

  4. State the range of integers representable in n-bit unsigned and n-bit two's complement signed forms. / n-बिट अनसाइंड और n-बिट टू'ज़ कॉम्प्लीमेंट साइंड रूपों में निरूपित किए जा सकने वाले पूर्णांकों की परास बताइए।
    Show answer

    Unsigned n-bit range is 0 to 2^n - 1; signed two's complement n-bit range is -2^(n-1) to 2^(n-1) - 1. / अनसाइंड n-बिट परास 0 से 2^n - 1 है; साइंड टू'ज़ कॉम्प्लीमेंट n-बिट परास -2^(n-1) से 2^(n-1) - 1 है।

  5. Calculate the uncompressed size (in MiB) of a 5-megapixel image with 24-bit colour. / 24-बिट रंग वाली 5-मेगापिक्सेल छवि का असम्पीडित आकार (MiB में) निकालिए।
    Show answer

    Size = 5,000,000 x 24 / 8 = 15,000,000 bytes; dividing by 1024 twice gives about 14.31 MiB. / आकार = 5,000,000 x 24 / 8 = 15,000,000 बाइट; 1024 से दो बार भाग देने पर लगभग 14.31 MiB मिलता है।

  6. Convert binary 111101 to octal using the grouping shortcut. / समूहीकरण शॉर्टकट का उपयोग करके बाइनरी 111101 को ऑक्टल में बदलिए।
    Show answer

    Group bits in threes from the right: 111 and 101, which map to 7 and 5, giving 75 in octal. / दाईं ओर से तीन-तीन बिटों में समूहित करें: 111 और 101, जो 7 और 5 में बदलते हैं, अतः ऑक्टल में 75 मिलता है।

  7. In IEEE 754 single precision, what do the three fields represent and what is the exponent bias? / IEEE 754 सिंगल प्रिसिजन में तीन फ़ील्ड क्या दर्शाते हैं और एक्सपोनेंट बायस क्या है?
    Show answer

    The 1 sign bit gives the number's sign, the 8 exponent bits store a biased exponent, and the 23 fraction bits store the mantissa; the bias for single precision is 127. / 1 साइन बिट संख्या का चिह्न देता है, 8 एक्सपोनेंट बिट एक बायस्ड एक्सपोनेंट संग्रहीत करते हैं, और 23 फ्रैक्शन बिट मैंटिसा संग्रहीत करते हैं; सिंगल प्रिसिजन के लिए बायस 127 है।

  8. Distinguish between integer overflow and floating-point overflow with their typical outcomes. / पूर्णांक ओवरफ्लो और फ्लोटिंग-पॉइंट ओवरफ्लो में उनके सामान्य परिणामों सहित अंतर कीजिए।
    Show answer

    Integer overflow occurs when a result exceeds the fixed bit range and typically wraps around (modular result), while floating-point overflow occurs when the exponent exceeds the maximum and the result is typically set to plus or minus infinity. / पूर्णांक ओवरफ्लो तब होता है जब परिणाम निश्चित बिट परास से अधिक हो जाता है और सामान्यतः रैप-अराउंड (मॉड्यूलर परिणाम) होता है, जबकि फ्लोटिंग-पॉइंट ओवरफ्लो तब होता है जब एक्सपोनेंट अधिकतम से अधिक हो जाता है और परिणाम सामान्यतः धन या ऋण अनंत में सेट हो जाता है।

Related Laws & Principles

Explore all

Foundational laws & principles connected to this chapter — tap to open in the Laws Explorer.

Loading related laws…
Sourced from 177 content files · LLOS Learn · browse all chapters