Computer Data Representation: Binary, Integers, Floating Point

Binary Encoding Fundamentals

  • Binary (Base 2): Each digit is 0 or 1.
  • Hexadecimal (Base 16): A compact representation of binary.
    • Example: 0x69B53A = 01101001 10110101 00111010 in binary.
  • Memory Addressing:
    • Big Endian: The most significant byte is stored at the lowest memory address.
    • Little Endian: The least significant byte is stored at the lowest memory address.

Unsigned Integers (B2U Encoding)

  • Range: 0 to 2^w - 1 (where w is the number of bits).
  • Addition: u + v mod 2^w (overflow discards the carry bit).
  • Subtraction: u - v = u + (2^w - v).
  • Overflow: Results in modular arithmetic, wrapping around.

Signed Integers (Two’s Complement – B2T Encoding)

  • Range: -2^(w-1) to 2^(w-1) - 1.
  • Negation: Complement all bits and then add 1.
  • Addition/Subtraction: Performed similarly to unsigned arithmetic, but correctly handles negative values.
  • Overflow: Can occur if the result exceeds the representable range for signed numbers.

Bit Shift Operations

  • Left Shift (<<): Multiplies the number by 2^k (fills with 0s on the right).
    • Example: u << 3 = u * 8.
  • Right Shift (>>):
    • Logical Shift (for unsigned numbers): Fills the vacated bits with 0s.
    • Arithmetic Shift (for signed numbers): Replicates the sign bit to preserve the number’s sign.
  • Undefined Behavior: Occurs if the shift amount is less than 0 or greater than or equal to the word size.

IEEE 754 Floating Point Representation

  • Components:
    • Sign bit (s): 0 for positive, 1 for negative.
    • Exponent (E): Represented in a biased format (E = exp - Bias).
    • Significand (M): The fractional part, with an implied leading 1 for normalized numbers.
  • Normalized Values: Occur when exp ≠ 000…0 and exp ≠ 111…1.
    • Value: v = (-1)^s * M * 2^E.
  • Denormalized Values: Occur when exp = 000…0.
    • Represent very small numbers close to 0.
  • Special Values:
    • If exp = 111…1 and frac = 000…0 → Represents Infinity (±∞).
    • If exp = 111…1 and frac ≠ 000…0 → Represents NaN (Not a Number).

Floating Point Arithmetic Operations

  • Addition:
    • Align exponents, add significands, then normalize the result.
    • Overflow leads to ±∞ (Infinity).
  • Multiplication:
    • Multiply significands, add exponents, then normalize the result.
    • Overflow leads to ±∞ (Infinity).
  • Properties:
    • Commutative: Yes, for both addition and multiplication.
    • Associative: No, due to potential rounding errors.
    • Distributive: No, due to potential rounding errors.

Floating Point Precision and Rounding

  • Precision Limitations:
    • Fractional binary numbers can only exactly represent numbers of the form x/2^k.
    • Example: Decimal fractions like 1/3, 1/5, and 1/10 cannot be exactly represented in binary floating-point.
  • Rounding Methods:
    • Round to Even: A common method that helps prevent cumulative rounding errors by rounding to the nearest even number when a value is exactly halfway between two integers.
    • Example: 1.5 rounds to 2, and 2.5 rounds to 2.

Casting Between Data Types in C

  • Explicit Casting:
    • tx = (int) ux; (Casting an unsigned integer to a signed integer).
    • uy = (unsigned) ty; (Casting a signed integer to an unsigned integer).
  • Same Bit Vector, Different Meanings:
    • Example: The bit pattern 1101 can represent 13 (as an unsigned integer) or -3 (as a signed two’s complement integer), depending on the interpretation.

Essential Data Representation Formulas

  • Unsigned Addition: s = UAdd_w(u, v) = (u + v) mod 2^w.
  • Two’s Complement Negation: -x = ~x + 1 (bitwise NOT x, then add 1).
  • Floating Point Value (IEEE 754): v = (-1)^s * M * 2^E.

Common Data Representation Pitfalls

  • Integer Overflow: Can lead to incorrect values or undefined behavior in programs.
  • Floating Point Rounding Errors: Floating point arithmetic is inherently not exact, leading to precision issues.
  • Invalid Bit Shifts: Performing bit shifts with an amount less than 0 or greater than or equal to the word size results in undefined behavior.