why does the modula of `x mod 2 to k power` will retain k bits

ChatGPT:

The formula $x’ = x \mod 2^k$ describes the process of truncating a larger bit vector $x$ (with $w$ bits) down to $k$ bits. Let’s break it down step by step to understand how and why this works.


1. Bit Vector Representation

  • Let $x$ be a $w$-bit binary number, represented as:

    $$x = [X_{w-1}, X_{w-2}, \dots, X_0]$$

    Here:

    • $X_{w-1}$ is the most significant bit (MSB).
    • $X_0$ is the least significant bit (LSB).
    • Each bit $X_i$ represents a binary digit (0 or 1).
  • The numerical value of $x$ in base 10 (assuming it is unsigned) is:

    $$x = X_{w-1} \cdot 2^{w-1} + X_{w-2} \cdot 2^{w-2} + \dots + X_0 \cdot 2^0$$

2. Truncating to $k$-Bits

  • Truncating $x$ to $k$-bits means keeping only the lower $k$ bits of the bit vector:

    $$x’ = [X_{k-1}, X_{k-2}, \dots, X_0]$$

    The higher-order bits $[X_{w-1}, \dots, X_k]$ are discarded.

  • The value of $x’$ in base 10 is:

    $$x’ = X_{k-1} \cdot 2^{k-1} + X_{k-2} \cdot 2^{k-2} + \dots + X_0 \cdot 2^0$$

3. Understanding $x’ = x \mod 2^k$

The modulus operation $x \mod 2^k$ keeps only the remainder when $x$ is divided by $2^k$. In binary terms, this is equivalent to ignoring all bits above the $k$-th bit.

  • The higher-order bits of $x$ (those with weights $2^i$ for $i \geq k$) do not contribute to the result because their weights are multiples of $2^k$, and:

    $$(2^i) \mod 2^k = 0 \quad \text{for all } i \geq k$$

  • Therefore, $x \mod 2^k$ retains only the lower $k$ bits of $x$, which is exactly what $x’$ represents after truncation.


4. Intuition with an Example

Suppose:

  • $x = 27$ (in decimal) or $11011_2$ (5 bits).
  • We want to truncate $x$ to $k = 3$ bits.

Step 1: Truncate to 3 bits

  • Retain only the lower 3 bits: $x’ = 011_2$, which is $3$ in decimal.

Step 2: Compute $x \mod 2^3$

  • $2^3 = 8$.
  • $x \mod 8 = 27 \mod 8 = 3$.

Result:

  • The truncated value $x’ = 3$ matches $x \mod 2^k$.

5. Why This Works

The principle works because in binary arithmetic:

  • Modulus by $2^k$ eliminates all bits at positions $k$ and higher, leaving only the lower $k$ bits.
  • Truncating a bit vector to $k$ bits achieves the exact same result by simply discarding the higher-order bits.

This is why the truncated value $x’$ is equivalent to $x \mod 2^k$.

Licensed under CC BY-NC-SA 4.0
Last updated on Dec 05, 2024 21:28 +0800