CSS Prepare

optional · CSS study notes

Data Representation

CSS Optional — Paper-II, Section-A · Chapter 7 · 🔥 Guaranteed numerical topic

Computers store everything — numbers, text, images, sound — as patterns of bits. Data representation is the study of the encoding conventions that assign meaning to those patterns. The same 8 bits 11111011 can mean 251, or −5, or a character, depending entirely on the convention in use.

🗺️ Architecture Diagram — Data Representation

graph TD
    N["Binary Pattern 11111011"] --> A["As unsigned: 251"]
    N --> B["As 2's complement signed: -5"]
    subgraph IEEE754["IEEE 754 Single Precision - 32 bits"]
        S["Sign
1 bit"] --> E["Exponent - biased
8 bits"] --> M["Mantissa
23 bits"]
    end

1. Number Systems

SystemBaseDigits usedCommon use
Binary20, 1Internal machine representation
Octal80–7Legacy; Unix file permissions
Decimal100–9Human interface
Hexadecimal160–9, A–FCompact display of binary (memory addresses, colour codes)

Why hexadecimal matters: one hex digit maps to exactly four binary bits, so a 32-bit address collapses into 8 readable hex digits with no arithmetic required.

Binary   : 1010 1100 1111 0001
Hex      :   A    C    F    1     →  0xACF1

2. Conversions

Decimal → Binary (repeated division by 2, read remainders bottom-up):

45 ÷ 2 = 22 r 1  ↑
22 ÷ 2 = 11 r 0  │
11 ÷ 2 =  5 r 1  │  read
 5 ÷ 2 =  2 r 1  │  upward
 2 ÷ 2 =  1 r 0  │
 1 ÷ 2 =  0 r 1  ↑
                    45₁₀ = 101101₂

Binary → Decimal (positional weights):

101101₂ = 1×32 + 0×16 + 1×8 + 1×4 + 0×2 + 1×1
        = 32 + 8 + 4 + 1 = 45₁₀

Fractional decimal → binary (repeated multiplication by 2, read integer parts downward):

0.625 × 2 = 1.25  → 1  ↓
0.25  × 2 = 0.5   → 0  │  read
0.5   × 2 = 1.0   → 1  ↓  downward
                       0.625₁₀ = 0.101₂

3. Signed Number Representation

Three conventions exist; only the third is used in practice.

(a) Sign-Magnitude

The leftmost bit is the sign (0 = positive, 1 = negative); the rest is the magnitude.

  • 8-bit range: −127 to +127
  • ❌ Two representations of zero (00000000 = +0, 10000000 = −0)
  • ❌ Requires separate subtraction circuitry

(b) 1's Complement

Negate by inverting every bit.

  • 8-bit range: −127 to +127
  • ❌ Still has two zeros (00000000 and 11111111)
  • ❌ Requires "end-around carry" correction after addition

(c) 2's Complement ⭐ (used by all modern computers)

Negate by inverting every bit, then adding 1.

  • 8-bit range: −128 to +127 (asymmetric — one extra negative value)
  • ✅ Exactly one representation of zero
  • ✅ Subtraction becomes addition — the same adder circuit handles both, which is the decisive engineering advantage

General n-bit 2's complement range:

−2^(n−1)  to  +2^(n−1) − 1
n bitsRange
8−128 to +127
16−32,768 to +32,767
32−2,147,483,648 to +2,147,483,647

4. IEEE 754 Floating-Point

Real numbers are stored in a normalised scientific form: ± 1.M × 2^E

PrecisionTotal bitsSignExponentMantissaBias
Single321823127
Double64111521023
  • Biased exponent — the bias is added so the stored exponent field is always non-negative, avoiding a separate sign bit for the exponent and allowing exponents to be compared as plain unsigned integers.
  • Implicit leading 1 — since every normalised binary number starts with 1., that bit is not stored, buying one extra bit of precision for free.

5. Character Encoding

  • ASCII — 7-bit, 128 characters. 'A' = 65, 'a' = 97, '0' = 48. (Remember: uppercase and lowercase differ by exactly 32.)
  • Extended ASCII — 8-bit, 256 characters.
  • Unicode / UTF-8 — variable length (1–4 bytes), covering every writing system including Urdu and Arabic. UTF-8 is backward-compatible with ASCII: the first 128 code points are byte-identical.
  • BCD (Binary Coded Decimal) — each decimal digit stored in its own 4 bits. Wastes space but avoids binary-to-decimal rounding errors, so it is still used in financial systems.

🧮 Solved Numerical 1 — 2's Complement Representation

Q: Represent −5 in 8-bit 2's complement.

Step 1: +5 in binary        →  0000 0101
Step 2: invert all bits     →  1111 1010
Step 3: add 1               →  1111 1011

Answer: −5 = 11111011

Verify: add +5 and −5; the result must be zero.

  0000 0101
+ 1111 1011
─────────────
1 0000 0000   → discard the carry out → 0000 0000 ✓

🧮 Solved Numerical 2 — Converting Back

Q: What decimal value does 11110001 hold in 8-bit 2's complement?

The leading bit is 1, so the number is negative. Apply 2's complement again to find its magnitude:

11110001  → invert →  00001110
          → add 1  →  00001111 = 15

Answer: −15

🧮 Solved Numerical 3 — Range Determination

Q: What is the range of a 16-bit 2's complement number, and how many distinct values can it hold?

Range = −2^(16−1)  to  2^(16−1) − 1
      = −2¹⁵ to 2¹⁵ − 1
      = −32,768 to +32,767

Distinct values = 2¹⁶ = 65,536

Answer: −32,768 to +32,767, holding 65,536 distinct values.

Note the asymmetry: there is a −32,768 but no +32,768, because one bit pattern is consumed by zero.

🧮 Solved Numerical 4 — IEEE 754 Single Precision

Q: Represent 10.0 in IEEE 754 single-precision format.

Step 1: Convert to binary
        10.0₁₀ = 1010.0₂

Step 2: Normalise to 1.M × 2^E
        1010.0 = 1.010 × 2³        →  E = 3, M = 010

Step 3: Sign bit
        Positive → S = 0

Step 4: Biased exponent
        Stored exponent = E + bias = 3 + 127 = 130
        130₁₀ = 1000 0010₂

Step 5: Mantissa (23 bits, drop the implicit leading 1)
        010 0000 0000 0000 0000 0000

Assembled result:

S  Exponent    Mantissa
0  10000010    01000000000000000000000

Answer: 0 10000010 01000000000000000000000 = 0x41200000

🧮 Solved Numerical 5 — Negative Float

Q: Represent −6.5 in IEEE 754 single precision.

Step 1: 6.5₁₀ = 110.1₂
Step 2: Normalise → 1.101 × 2²      →  E = 2
Step 3: Sign = 1 (negative)
Step 4: Exponent = 2 + 127 = 129 = 1000 0001₂
Step 5: Mantissa = 101 0000 0000 0000 0000 0000

Answer: 1 10000001 10100000000000000000000

🧮 Solved Numerical 6 — Storage Requirement

Q: A text file contains 5,000 characters stored in extended ASCII (8-bit). How much storage is required? What if stored as UTF-16?

ASCII  : 5,000 × 8 bits  = 40,000 bits  = 5,000 bytes  ≈ 4.88 KB
UTF-16 : 5,000 × 16 bits = 80,000 bits  = 10,000 bytes ≈ 9.77 KB

Answer: 5,000 bytes in ASCII; 10,000 bytes in UTF-16 — exactly double. This is why UTF-8 is preferred for English-dominant text: it uses one byte for ASCII characters while still supporting all Unicode.


🎯 CSS Exam Angle

2's complement conversion is one of the most reliably repeated numericals in the paper. IEEE 754 has become more common in recent attempts. Full marks require:

  1. Showing every intermediate step (binary form → invert → add 1), not just the answer.
  2. Verifying the result where possible (add the number to its negation, expect zero).
  3. For IEEE 754, laying out the three fields separately and labelled before assembling.

🔁 Repeated CSS Questions

  1. Convert a given decimal number to its 2's complement binary representation. (Numerical — very frequently repeated)
  2. Explain IEEE 754 floating-point representation with a worked example. (Frequently repeated)
  3. Differentiate between sign-magnitude, 1's complement, and 2's complement.
  4. Why is 2's complement preferred over 1's complement in modern computers?
  5. Determine the range of an n-bit signed number. (Numerical)
  6. Differentiate between ASCII and Unicode.
  7. Differentiate between fixed-point and floating-point representation.

⚠️ MCQ Traps

  • 2's complement has only ONE zero. Sign-magnitude and 1's complement each have two (+0 and −0). This single fact answers a large share of MCQs on this topic.
  • The 2's complement range is asymmetric — for 8 bits, −128 to +127, not −127 to +127. The extra negative value exists because zero occupies a positive-side pattern.
  • The IEEE 754 exponent is biased, not 2's complement. Add the bias (127 for single, 1023 for double); do not negate.
  • The mantissa's leading 1 is implicit and not stored. Candidates often store it and get 22 usable bits wrong.
  • A carry-out is not the same as overflow. In 2's complement addition, the final carry out of the sign position is simply discarded; overflow is a separate condition (see Arithmetic Microprocessor).
  • Hexadecimal is a display convenience, not a storage format. Memory holds binary; hex is how humans read it.