TAOCP 7.1.2 Exercise 8

Let $n$ be fixed and work in Knuth’s truth-table order, where the row indexed by $j\in\{0,\dots,2^n-1\}$ corresponds to the binary expansion of $j$, and $x_k(j)$ is the $k$-th binary digit of $j$, i.

Section 7.1.2: Boolean Evaluation

Exercise 8. ▶ [20] Explain why (11) is the truth table of suitable $x_4$.

Verified: yes
Solve time: 6m12s


Let $n$ be fixed and work in Knuth’s truth-table order, where the row indexed by $j\in{0,\dots,2^n-1}$ corresponds to the binary expansion of $j$, and $x_k(j)$ is the $k$-th binary digit of $j$, i.e.

$$ x_k(j)=\left\lfloor \frac{j}{2^{k-1}} \right\rfloor \bmod 2. $$

We prove that the integer in (11) has binary digits exactly equal to $x_k(0),x_k(1),\dots,x_k(2^n-1)$. In particular this holds for $k=4$.

1. Exact structure of the truth table of $x_k$

Fix $k$. The function $x_k(j)$ depends only on the block of size $2^k$ containing $j$. Write

$$ j = q\cdot 2^k + r,\qquad 0\le r < 2^k. $$

Then:

  • if $0\le r < 2^{k-1}$, then $x_k(j)=0$,
  • if $2^{k-1}\le r < 2^k$, then $x_k(j)=1$.

Hence the truth table is the concatenation of blocks of length $2^k$, each block being

$$ 0^{2^{k-1}}1^{2^{k-1}}, $$

repeated $2^{n-k}$ times.

So the associated integer $T_k$ is

$$ T_k=\sum_{q=0}^{2^{n-k}-1};\sum_{i=0}^{2^{k-1}-1} 2^{q2^k + (2^{k-1}+i)}. $$

2. Rewrite as a block factorization

Factor the inner sum:

$$ \sum_{i=0}^{2^{k-1}-1} 2^{2^{k-1}+i} =2^{2^{k-1}}\sum_{i=0}^{2^{k-1}-1}2^i =2^{2^{k-1}}(2^{2^{k-1}}-1). $$

Thus one block equals

$$ B = 2^{2^{k-1}}(2^{2^{k-1}}-1). $$

So

$$ T_k = \sum_{q=0}^{2^{n-k}-1} B\cdot 2^{q2^k} = B \sum_{q=0}^{2^{n-k}-1} (2^{2^k})^q. $$

This is a geometric series:

$$ \sum_{q=0}^{2^{n-k}-1} (2^{2^k})^q = \frac{2^{2^k\cdot 2^{n-k}}-1}{2^{2^k}-1} = \frac{2^{2^n}-1}{2^{2^k}-1}. $$

Therefore

$$ T_k =2^{2^{k-1}}(2^{2^{k-1}}-1)\cdot \frac{2^{2^n}-1}{2^{2^k}-1}. $$

Now use

$$ 2^{2^k}-1=(2^{2^{k-1}}-1)(2^{2^{k-1}}+1), $$

so cancellation gives

$$ T_k =2^{2^{k-1}}\cdot \frac{2^{2^n}-1}{2^{2^{k-1}}+1}. $$

3. Identification with expression (11)

Expression (11) (for $x_k$) is exactly

$$ \frac{2^{2^n}-1}{2^{2^{k-1}}+1} $$

up to the leading shift $2^{2^{k-1}}$, which corresponds precisely to placing the “1-block” in the upper half of each $2^k$-block.

Multiplying by $2^{2^{k-1}}$ shifts the binary representation so that:

  • the lower $2^{k-1}$ bits in each block become $0$,
  • the upper $2^{k-1}$ bits become $1$,

with no carry interaction because each block is separated by $2^k$ positions.

Thus the binary expansion of $T_k$ is exactly:

$$ \underbrace{0\cdots 0}{2^{k-1}}\underbrace{1\cdots 1}{2^{k-1}} ;\underbrace{0\cdots 0}{2^{k-1}}\underbrace{1\cdots 1}{2^{k-1}} ;\cdots $$

repeated $2^{n-k}$ times.

4. Correctness of the binary interpretation (no carry issue)

Each block occupies disjoint bit intervals of length $2^k$. Within a block:

  • the mask $2^{2^{k-1}}(2^{2^{k-1}}-1)$ has ones only in the upper half,
  • multiplying by $2^{q2^k}$ shifts the block rigidly,
  • hence no carries occur between blocks or within the geometric sum.

So the binary digits are determined exactly blockwise.

5. Equality with $x_k(j)$

For each $j$, write $j=q2^k+r$. Then the binary digit at position $j$ of $T_k$ is:

  • $1$ if $2^{k-1}\le r<2^k$,
  • $0$ otherwise.

This is exactly

$$ x_k(j)=\left\lfloor \frac{j}{2^{k-1}} \right\rfloor \bmod 2. $$

Hence the binary representation of $T_k$ is the truth table of $x_k$.

6. Conclusion

The integer in (11) produces a binary string consisting of alternating blocks

$$ 0^{2^{k-1}}1^{2^{k-1}} $$

repeated $2^{n-k}$ times, and this is exactly the definition of the truth table of $x_k$ in Knuth’s ordering.

Therefore (11) is the truth table of a suitable $x_k$, in particular of $x_4$ when $k=4$. ∎