Skip to content

Week 2

Data Representation & Number Systems

Computers operate using binary (base 2), so we often need to convert between number systems and represent integers and real numbers efficiently.


Number Bases

System Base Digits Used Example
Decimal 10 0–9 245₁₀
Binary 2 0–1 111101₂
Octal 8 0–7 725₈
Hexadecimal 16 0–9, A–F A3F₁₆

Conversions Between Bases

Decimal → Binary (Repeated division by 2)

  1. Divide by 2.
  2. Record remainder.
  3. Continue until quotient = 0.
  4. Binary result = remainders read bottom → top.
Example: 224₁₀ → Binary
224 ÷ 2 = 112    remainder 0
112 ÷ 2 = 56     remainder 0
56 ÷ 2 = 28      remainder 0
28 ÷ 2 = 14      remainder 0
14 ÷ 2 = 7       remainder 0
7 ÷ 2 = 3        remainder 1
3 ÷ 2 = 1        remainder 1
1 ÷ 2 = 0        remainder 1

11100000₂   (comes from the remainders)

Binary → Decimal (Positional weighting)

(aₙ … a₃a₂a₁a₀)₂ = Σ aᵢ · 2ⁱ

Example: 1101₂ → Decimal

1101₂

i₃ i₂ i₁ i₀
Bit value aᵢ 1 1 0 1
Term aᵢ·2ⁱ 1×2³ 1×2² 0×2¹ 1×2⁰
Value 8 4 0 1

Therefore,
1101₂ = 8 + 4 + 0 + 1 = 13₁₀


Binary ↔ Hexadecimal (Group by 4 bits)

Example: 110101101001₂ → Hexadecimal
Binary (4-bit groups) 1101 0110 1001
Hexadecimal D 6 9
Example: AF2₁₆ → Binary
Hexadecimal A F 2
Binary (4-bit groups) 1010 1111 0010

-

Representing Integers in Binary

Unsigned Integers

With N bits, values range from: [ 0 \text{ to } (2^N - 1) ]

Bits Range
8-bit 0 → 255
16-bit 0 → 65,535
32-bit 0 → ~4.29 billion

Signed Integers (Two’s Complement)

To negate a number:

  1. Invert each bit.
  2. Add 1.
Example: Represent −24₁₀ using two's complement
   +24 = 00011000₂
invert → 11100111₂
    +1 → 11101000₂
         -------- 
   −24 = 11101000₂

Two’s Complement Integer Range

Formula: −(2ⁿ⁻¹) → (2ⁿ⁻¹ − 1)

Bits Range
8-bit −128 → +127
16-bit −32768 → +32767

Subtraction Using Two’s Complement

Formula: a - b = a + (-b)

Example: 3 − 13 using two's complement
       3 = 00000011₂  

     +13 = 00001101₂  
  invert → 11110010₂  
      +1 → 11110011₂  
           ────────  
     −13 = 11110011₂


       3 = 00000011₂  
 + (−13) = 11110011₂
           --------
         = 11110110₂ 

Since the result begins with 1, it is negative.  
Take the two’s complement again to find its value:

3 + (-13) = 11110110₂ 
   invert → 00001001₂ 
       +1 → 00001010₂ = −10₁₀

Bit width must be sufficient

Two’s complement arithmetic only works correctly when the chosen bit width can represent both operands and their result. If the result falls outside the representable range (e.g. outside −128 to +127 for 8-bit signed integers), overflow occurs and the computed value will be incorrect.


Floating-Point Numbers (IEEE 754): A Natural Analogy to Scientific Notation

Computers represent real numbers using floating-point format, which works like scientific notation—but in binary instead of decimal.
This lets us efficiently store both very large and very small numbers using a fixed number of bits.


1. Scientific Notation: The Decimal Version

In decimal, numbers can be written in scientific notation as:

(−1)ˢ × m × 10ᴱ

where:

  • S – sign (0 = positive, 1 = negative)
  • m – significand (or mantissa), with exactly one non-zero digit before the decimal point
  • E – exponent, the power of ten that scales the significand

Example

−5470 = (−1)¹ × 5.47 × 10³

This is the normalized form, ensuring a single non-zero digit appears before the decimal point.


2. Binary Scientific Notation: Same Idea, Base-2

The same idea applies in binary:

(−1)ˢ × m × 2ᴱ

where:

  • S – sign bit (0 = positive, 1 = negative)
  • m – significand, representing the significant binary digits
  • E – exponent, the power of two that scales the significand

In binary, the only possible non-zero digit is 1, so all normalized numbers have:

m = 1.F

where F is the fractional part after the binary point.

The general form becomes:

(−1)ˢ × (1.F) × 2ᴱ
When storing these numbers, we can drop constants that never change.
Therefore we only need to store:

  • S – sign bit
  • E – exponent
  • F – fraction bits

3. The Exponent Bias

Exponents can be positive or negative, but hardware stores unsigned binary values. To handle both, IEEE 754 adds a bias so all exponents fit in a positive range.

Format Exponent Bits Bias Formula Bias Value
Single 8 2⁷ − 1 127
Double 11 2¹⁰ − 1 1023

The stored exponent is:

Eᵦ = E + Bias

Example

If E = −1 (single precision)
Eᵦ = −1 + 127 = 126 = 01111110₂

4. IEEE 754 Bit Layout

Each floating-point number is divided into three fields, each with a fixed bit length:

Precision (Format) Total Bits Sign Field (bits) Exponent Field (bits) Fraction Field (bits)
Single (float) 32 1 8 23
Double (double) 64 1 11 52
Example – Encoding −0.75₁₀ as a 32-bit Float

Step 1 – Include the sign and convert to binary

The number is negative, so the sign bit (S) will be 1.

Convert the magnitude (0.75) to binary by repeatedly multiplying by 2 and recording each carry:

0.75 × 2 = 1.5 → 1
0.50 × 2 = 1.0 → 1

So the positive part is:

0.75₁₀ = 0.11₂

Add the sign and express it in binary scientific notation:

−0.75₁₀ = (−1)¹ × 1.1₂ × 2⁻¹

Step 2 – Identify the fields

Component Description Value
S Sign bit 1 (negative)
E Exponent −1
F Fraction 1

Step 3 – Apply the bias

Eᵦ = E + Bias = −1 + 127 = 126 = 01111110₂

Step 4 – Assemble the fields

Sign Exponent Fraction
1 01111110 10000000000000000000000

*note that the F is padded with 0s to fill all 23 bits

Step 5 – Final 32-bit representation

10111111010000000000000000000000₂