Lecture 4 Part 1 — Comprehensive Notes on Computer Data Representation (Unsigned vs Two's Complement; Shifts; Division by Powers of Two)

Course Information and Study Guidelines

  • Lecturer: Prof. Soraya Abad-Mota, PhD; Course: Computer Data Representation (3) – Lecture #4 Part 1.
  • Page 2 emphasizes participation requirements:
    • Write name and date at the top; date and participation status are tracked.
    • Empty cards do not count as participation.
    • You should write something meaningful:
    • If no new insights: comment on what you learned.
    • If you already knew the material: answer at least one of the exercises done during the lecture (you must do them during the lecture to aid learning).
    • Reflect on something interesting from the lecture.
  • This set of notes reflects the instructional prompts and exercises to reinforce understanding of data representation, unsigned vs signed numbers, and operations with powers of two.

Part I: Data Representation — Integral Types, Signed and Unsigned (Overview)

  • Why do we discuss unsigned vs two’s complement vs signed representations?
    • There is a relationship between two’s complement representation and unsigned interpretation that matters for arithmetic, overflow handling, and bit-level operations.
    • The same bit pattern can be interpreted as signed or unsigned depending on the context; this affects how addition, subtraction, and shifts behave.
  • Binary representation of 8 in two’s complement:
    • For a given word length (e.g., 8-bit example), the representation of the positive number 8 is the same in unsigned and two’s complement:
      8=0000100028 = 00001000_2
    • The same bit pattern, if interpreted as a signed value in two’s complement, yields 8 as well (since it is nonnegative and within the range).
  • Why add/subtract 2^n when moving between signed two’s complement and unsigned interpretations:
    • In an n-bit word, wrapping around (modulo arithmetic) occurs with unsigned arithmetic:
      a+b mod 2na + b \bmod 2^n
    • A negative value in two’s complement corresponds to an unsigned value equal to its bit pattern plus 2^n. In other words, the same bit pattern represents a signed value s or an unsigned value u with a relation near the boundary of 2^n.
    • This is why there is a datatype for unsigned; you must explicitly choose the interpretation to avoid erroneous math when wrapping occurs.
  • Overflow adjustment:
    • In two’s complement, overflow behavior is handled modulo 2^n; values wrap around rather than saturate. As a result, arithmetic operations can overflow without raising a hardware exception.
  • Datatype distinctions:
    • There is a dedicated datatype for unsigned integers in many languages (e.g., C/C++). Using the unsigned type changes arithmetic semantics (notably wrapping/modular properties) and the behavior of comparisons and shifts.
  • Quick reference idea: two’s complement or unsigned have distinct, well-defined semantics for overflow, wraparound, and bit-level operations; choose the type that matches the intended mathematical model.

Part II: When and Why to Use Unsigned vs Signed

  • Why use unsigned?
    • Escalates modular arithmetic guarantees: unsigned addition and subtraction wrap around on overflow (mod 2^n).
    • Useful for bitwise operations, counting, indexing (e.g., array lengths, bit masks), and logical right shifts without sign extension.
  • When to specify two’s complement vs unsigned? There is a datatype distinction:
    • Use unsigned when you conceptually model quantities that cannot be negative (counts, sizes, bit masks).
    • Use signed (two’s complement) when modeling quantities that may be negative (differences, net gains/losses).
  • Floating point representation (Part 2) is introduced in the course but not covered in this Part 1 material.

Part III: Common Pitfalls with Unsigned in C/C++ (or similar languages)

  • Key warning: unsigned arithmetic and loops can be subtly wrong if the loop bounds are not chosen carefully.

  • Example of a common mistake (dangerous with unsigned indices):

    • Code pattern:

      ext{for (i = cnt-2; i >= 0; i--)}

      a[i] += a[i+1];

    • If i is unsigned, the condition i >= 0 is always true (since i can’t go below 0), leading to wrap-around once i becomes large again. This misleads about termination and can cause out-of-bounds access.

  • A more subtle approach shown in the slides uses DELTA and sizeof(int) to advance in steps:

    • Example snippet (schematic):

      define DELTA sizeof(int)

      int i;
      for (i = CNT; i-DELTA >= 0; i -= DELTA)
      . . .


    • The point is to avoid assuming signed negativity or controlled termination with unsigned counters.
  • The recommended caution: always consider wrap-around behavior and the direction of iteration when using unsigned indices.

Part IV: Proper Use of Unsigned as Loop Indices (Best Practices)

  • Correct pattern with unsigned for loops:
    • Example from the slides (presented as guidance):

      unsigned i;
      for (i = cnt-2; i < cnt; i--)
      a[i] += a[i+1];
    • This pattern relies on wrap-around properties of unsigned integers to terminate when i becomes large again. It is used to perform backward iteration safely in some contexts.
  • Safer pattern using size_t (the standard unsigned type for sizes):
    • Replace unsigned with sizet for clarity about size semantics: sizet i;
      for (i = cnt-2; i < cnt; i--)
      a[i] += a[i+1];
  • Rationale for size_t:
    • size_t is defined as an unsigned type with length equal to the word size of the platform.
    • The code will work even if cnt equals UMax (the maximum unsigned value).
  • Important question: What if cnt is signed and negative?
    • If cnt is signed and negative, the behavior of loops that wrap with unsigned indices can lead to logical errors or runtime issues; ensure proper type choices align with the intended domain (nonnegative counts).
  • Practical takeaway:
    • Use unsigned types for modular arithmetic and bit-level representations.
    • Prefer size_t for sizes and counts that must not be negative, but be careful with loop termination conditions when iterating downward.

Part V: When to Use Unsigned for Special Purposes

  • Do Use When Performing Modular Arithmetic:
    • Arithmetic modulo 2^n (wrap-around).
    • Useful in algorithms that rely on wraparound properties (e.g., circular buffers, hash computations with natural wrap).
  • Do Use When Using Bits to Represent Sets:
    • Bit masks, bit fields, and set representations benefit from unsigned types and shift operations.
    • Logical right shifts are important when you want to avoid sign-extension issues that occur with arithmetic right shifts on signed integers.
  • These uses align with the property that unsigned arithmetic is well-defined under modulo arithmetic in most languages (e.g., C/C++ standards).

Part VI: Bit Shifts and Division by Powers of Two (Foundational Concepts)

  • The basic idea: dividing by a power of two is often done with bit shifts.
  • Caveat: the operation is more nuanced for signed numbers than for unsigned numbers.
  • For unsigned numbers:
    • Shifting right by k bits is equivalent to division by 2^k with floor behavior, and there is no sign extension because numbers are nonnegative.
    • Example: for unsigned x, x ext{ >> } k = ig
      floor rac{x}{2^k} ig
      floor
  • For signed numbers (two’s complement):
    • Shifting right (arithmetic shift) preserves the sign bit; it effectively performs division by 2^k with rounding toward negative infinity for negative numbers.
    • The slides illustrate this with several examples (see detailed examples below).
  • Practical notes:
    • Shifting by a fixed power of two is not always equivalent to truncating toward zero for negative values. It depends on the sign of x and the direction of rounding.
    • When you need truncation toward zero, you often need to bias the dividend before shifting (or use explicit division operators and rounding controls).

Part VII: Quotients by Power of Two — Signed Numbers (Rounding Behavior)

  • Quotient of a signed number by a power of two using right shift:
    • Operation: xext>>kextgivesext”x/2k”(roundedtowardnegativeinfinityoften)x ext{ >> } k ext{ gives } ext{”x / 2^k” (rounded toward negative infinity often)}
    • This uses arithmetic right shift (sign-extended) and is correct for x ≥ 0, but for x < 0 the result can be rounded in the wrong direction if you expect rounding toward zero.
  • Concrete illustrative values (two’s complement behavior):
    • Example: for x = -15213 and k = 1,
    • x >> 1 = -7607, while x / 2 = -7606.5, so the shift rounds toward -∞ (i.e., to -7607).
    • Example: for x = -15213 and k = 4,
    • x >> 4 = -951, while x / 2^4 = -950.8125, rounding toward -∞ gives -951.
    • Example: for x = -15213 and k = 8,
    • x >> 8 = -60, while x / 2^8 = -59.4257813, rounding toward -∞ gives -60.
  • Summary:
    • x >> k equals floor(x / 2^k) for x ≥ 0.
    • For x < 0, x >> k rounds toward -∞, not toward zero.
  • Visual understanding (from the slide’s tabular representation):
    • The arithmetic right shift aligns with signed division by powers of two, but the rounding behavior differs depending on the sign of x.

Part VIII: Getting Division Right for Negative Numbers (Rounding Toward Zero)

  • If you need the quotient of x by 2^k rounded toward zero, you must bias the dividend before shifting.
  • In C: a common idiom is: (x+(1ext<<k)−1)ext>>k(x + (1 ext{<<} k) - 1) ext{ >> } k
    • Or equivalently, using an approach that biases toward zero:
    • This effectively ensures that negative numbers do not skew toward -∞ when you want truncation toward zero.
  • The slides describe two cases:
    • Case 1: No rounding (or standard division behavior under some conditions).
    • Case 2: Rounding toward zero (biasing dividend by 2^k - 1 before shifting).
  • Biasing toward zero has a significant effect on the final result for negative dividends; bias = 2^k - 1 helps emulate truncation toward zero in the shifted result.
  • Binary and decimal illustrations in the slides show how bias changes the final quotient and where the bias lands in the binary/digital representation.

Part IX: Practical Implications and Takeaways

  • Always be mindful of whether your integers are signed or unsigned and how that affects:
    • Loop termination conditions (especially when looping downward with unsigned counters).
    • Results of shifts and divisions by powers of two.
    • Overflow wraparound behavior (mod 2^n) and the lack of automatic exceptions.
  • When using unsigned as loop indices:
    • Prefer patterns that guarantee termination without relying on wrap-around in ways that can be hard to reason about.
    • Consider using size_t for indices and explicit boundary checks.
  • Use of modular arithmetic and bit-level representations can simplify certain algorithms (e.g., bitmasks, bit-field operations, multiprecision arithmetic), but require careful handling of sign and overflow semantics.
  • References and recommended readings:
    • Robert Seacord, Secure Coding in C and C++ (note the emphasis on safe unsigned arithmetic and wrapping behavior).
  • Final guidelines:
    • Always verify the intended rounding direction when dividing by powers of two for signed numbers.
    • When in doubt, implement explicit division and rounding logic rather than relying solely on bit shifts for negative values.
  • Conceptual analogies:
    • Modular arithmetic with unsigned integers is like a circular counter that wraps from UMax back to 0.
    • Sign extension in arithmetic right shifts mirrors carrying the sign bit through the shift operation.

Part X: Recap of Key Formulas and Concepts (LaTeX-formatted)

  • Relationship between signed and unsigned interpretations (n-bit word):
    s={x,0≤x<2n−1 x−2n,2n−1≤x<2ns = \begin{cases} x, & 0 \le x < 2^{n-1} \ x - 2^n, & 2^{n-1} \le x < 2^n \end{cases}
  • Arithmetic right shift for signed numbers (conceptual):
    x≫k≈floor(x2k)(for x≥0)x \gg k \approx \text{floor}\left(\frac{x}{2^k}\right) \quad \text{(for } x \ge 0\text{)}
  • For negative x, x≫k≈floor(x2k), often rounding toward −∞x \gg k \approx \text{floor}\left(\frac{x}{2^k}\right) \text{, often rounding toward } -\infty
  • Rounding toward zero for negative dividends by a power of two (C-style):
    (x+(1≪k)−1)≫k(x + (1 \ll k) - 1) \gg k
  • Modular arithmetic property of unsigned addition (wraparound):
    a+b≡(a+b) mod 2na + b \equiv (a + b) \,\mathrm{mod}\ 2^n
  • Size type rationale:
    • size_t is unsigned with length equal to the machine word size.
    • It is suitable for counting and indexing where the range is nonnegative.
  • Important caution:
    • The same bit pattern can represent different values depending on interpretation (unsigned vs two’s complement signed), which affects overflow, comparisons, and arithmetic semantics.

Part XI: Quick Study-Oriented Takeaways (for Exam Preparation)

  • Remember the difference between unsigned and signed (two’s complement) basics, especially when interpreting the same bit pattern.
  • Be able to explain why unsigned arithmetic wraps and how that affects loops and array indexing.
  • Know how to perform division by powers of two and how shifts relate to division, including the caveats for negative numbers.
  • If you need truncation toward zero for negative dividends, memorize the bias trick: add 2^k - 1 then shift (in C-like syntax: (x + (1 << k) - 1) >> k).
  • Understand why using size_t for iteration and indexing is safer than using plain int when counts can approach UMax, and know how to structure loops to avoid wrap-around pitfalls.
  • Be aware of referenced literature (e.g., Seacord) for secure coding practices related to unsigned arithmetic.