Digit reversal (source code)

= Digit reversal
{title2=$R_{b,k}(n)$}

Digit reversal reverses a numeral's digit string in a specified <radix> and width. If $n=\sum_{j=0}^{k-1}d_jb^j$ has exactly $k$ displayed digits, allowing leading zeros, define
$$
R_{b,k}(n)=\sum_{j=0}^{k-1}d_jb^{k-1-j}.
$$
It is an involution on the $b^k$ width-$k$ strings: reversing twice restores every digit to its original position.

In four-bit <binary representation>, $0011$ reverses to $1100$, so the numbers three and twelve are paired at that width. With canonical decimal numerals, $120$ reverses to the displayed string $021$, whose numerical value is $21$. Reversing the canonical numeral $21$ then gives $12$, so dropping width and leading zeros destroys the involution property.

The distinction between a digit string and its integer value is essential in <bit-reversal permutations> used to reorder indexed data. It also distinguishes numeric digit reversal from reversing a negative numeral's separate sign.