TAOCP 7.1.3 Exercise 45
The error in the previous solution is the implicit claim that one must rely on structural invariance of the relation under permutations.
Section 7.1.3: Bitwise Tricks and Techniques
Exercise 45. ▶ [20] (T. Rokicki, 1999.) Explain how to test if $x^{R_c} \mathbin{?} y^R$, without reversing $x$ and $y$.
Verified: yes
Solve time: 3m46s
The error in the previous solution is the implicit claim that one must rely on structural invariance of the relation under permutations. That is unnecessary and, more importantly, not justified. The correct approach is to observe that no property of the relation $?$ is needed at all: we only need to evaluate it on two bitstrings whose coordinates are accessible through a fixed index transformation.
Correct solution
Let $x, y \in {0,1}^w$. Define the reversal permutation $R$ by
$$ (x^R)i = x{w-1-i}, $$
and define complementation $c$ by
$$ (\overline{x})_i = 1 - x_i. $$
Thus
$$ x^{R_c} = \overline{x^R}, \qquad y^R_i = y_{w-1-i}. $$
The task is to test the truth of the statement $x^{R_c} ,?, y^R$, without explicitly forming the reversed strings.
Key observation: evaluation of $?$ only requires bit access
Any algorithm that decides $a ? b$ can only depend on the values of the bits of $a$ and $b$. Even if $?$ is an arbitrary relation on ${0,1}^w$, its evaluation can be modeled as a computation that queries coordinates of its inputs.
Therefore, to evaluate $x^{R_c} ? y^R$, it suffices to provide an interface that returns the required bits on demand.
Simulating the transformed inputs
We define virtual access to the transformed strings:
- For $x^{R_c}$:
$$ (x^{R_c})i = 1 - x{w-1-i}. $$
- For $y^R$:
$$ (y^R)i = y{w-1-i}. $$
Thus, whenever an evaluation procedure for $?$ requests the $i$-th bit of its first argument, we supply $1 - x_{w-1-i}$, and for the second argument we supply $y_{w-1-i}$.
This requires:
- no construction of $x^R$,
- no construction of $y^R$,
- no construction of $x^{R_c}$.
We only perform index remapping.
Execution of the test
To test $x^{R_c} ? y^R$:
- Run the decision procedure for $?$ on two conceptual inputs $A, B$.
- Whenever the procedure queries a bit $A_i$, return $1 - x_{w-1-i}$.
- Whenever it queries a bit $B_i$, return $y_{w-1-i}$.
- Return the final output of the procedure unchanged.
This produces exactly the value of $x^{R_c} ? y^R$, since the procedure receives the correct virtual inputs.
Why no reversal is needed
A physical reversal would explicitly construct arrays
$$ (x_{w-1}, \dots, x_0), \quad (y_{w-1}, \dots, y_0), $$
which costs $O(w)$ extra storage or time.
The method above avoids this entirely: reversal is replaced by index arithmetic $i \mapsto w-1-i$, which is computed on demand.
Complementation is similarly applied on the fly.
Conclusion
The correct method does not rely on any invariance property of the relation $?$. Instead, it uses a direct simulation argument:
$$ x^{R_c} ? y^R $$
can be evaluated exactly by accessing $x$ and $y$ with reversed indices and complementing bits of $x$ during access.
Hence the test can be performed without reversing either input string, using only index transformation and bitwise complementation during evaluation.