XOR-Based Metric Spaces π
The Hamming metric π
Let us fix a length $D$ and consider the set of all $D$-bit strings. We number bit positions from the right, starting at $0$: bit $D{-}1$ is the most significant (leftmost) bit, bit $0$ the least significant (rightmost) one. The $2^D$ strings are the points of our space. In many situations, we might be interested in measuring how close two such strings are. For example, a search engine might need to compare binary signatures of documents, and the quantized KV cache of an LLM to find the keys most similar to a query. And once a measure of distance is defined, we might also want the ability to move a string a little closer to another, or a little further away. When training an embedding, for instance, we pull similar items together and push dissimilar ones apart.
The classical metric space for bit strings is the Hamming space. The Hamming distance between two bit strings is the number of bit positions where they differ. Two bit-level operations make this computable in one line. The bitwise exclusive or (XOR, written $\oplus$) compares two strings position by position: each output bit is $1$ where the inputs differ and $0$ where they agree, so $x \oplus y$ is a difference mask, marking exactly the positions of disagreement. And $\mathrm{popcount}$ (population count) counts the $1$s in a bit string. Together, they define the Hamming distance: $$ d_H(x, y) := \mathrm{popcount}(x \oplus y). $$
For example, $\mathtt{0110} \oplus \mathtt{1010} = \mathtt{1100}$: the mask has two $1$s, so $d_H(\mathtt{0110}, \mathtt{1010}) = 2$, the strings disagreeing in their two leftmost positions (Figure 1.1). Note that the Hamming space is a perfectly good metric space, and its distance has a clean operational meaning. It counts the single-bit edits needed to turn one string into the other. For all its elegance, however, the Hamming geometry has three shortcomings that stand out:
Figure 1.1. The Hamming space at $D = 4$ as a graph. Each of the $2^4$ strings is a vertex of the graph, drawn here as two cubes side by side. Two vertices are adjacent exactly when their strings differ in one bit, so the Hamming distance between two strings is the number of edges on a shortest path between them. The example pair $\mathtt{0110}$ (blue) and $\mathtt{1010}$ (red) sits at distance $2$, and one of its two shortest paths, the one via $\mathtt{1110}$, is drawn in black.
- Between any two points, $d_H$ can take only the $D{+}1$ values $0, 1, \dots, D$. The difference mask $x \oplus y$ is itself a string of $D$ bits and could in principle distinguish $2^D$ cases. Yet $d_H$ encodes barely $\log_2(D{+}1)$ bits of that information. At $D = 4$ that leaves us with five distinguishable distances in a space of $16$ strings. While the mask records which positions differ, $d_H$ keeps only the count.
- Keeping only the count, $d_H$ cannot say where a string lies. All strings differing from a fixed string in $r$ positions sit at distance $r$, so a distance value picks out a whole shell of $\binom{D}{r}$ candidates rather than a single point. At $D = 4$, a distance of $2$ already fits six different strings. Only the extreme values locate a point uniquely, distance $0$ (the string itself) and distance $D$ (its complement).
- With so little information in the distance, measurement is coarse, and movement jumpy. The gentlest step available, a single-bit update, changes the distance to any reference point by one full unit, and no smaller nudge exists. At $D = 4$, one such step already covers a quarter of the entire scale. For learning, this coarseness is a real limitation. Training an embedding means nudging an item a little closer to similar ones and a little further from dissimilar ones, and a distance with only $D{+}1$ values has no room for such nudges. Yet the $D$ bits of the mask hold enough information for a distance of $2^D$ values, which would allow steps as small as $1/(2^D{-}1)$ of the scale.
Contrast this with the space we build in this post. The Graywalk metric uses all the information the mask carries, measuring distance on a scale of $2^D$ values. Every distance, measured from any fixed string, is achieved by exactly one string, so distance now locates a point rather than a shell. Movement along that coordinate is both fine-grained and exact, and a pull or a push of any prescribed number of units lands on a uniquely determined point. To understand how the Graywalk metric works, let us first take a closer look at the reflected binary code, better known as the Gray code.
The Graywalk metric π
Gray codes π
For what follows, it helps to read a bit string geometrically, as a root-to-leaf path in a complete binary tree of depth $D$ (Figure 1.2). Read left to right, each bit says whether the path turns left ($0$) or right ($1$) at that level, so bit $D{-}1$ is the first (root-level) decision and bit $0$ the last (leaf-level) one. The $2^D$ strings are then exactly the leaves of the tree. A bit string can also be read as a number. With the bits numbered from the right as above, $\mathtt{b_3 b_2 b_1 b_0}$ is the binary encoding of $b_3 \cdot 2^3 + b_2 \cdot 2^2 + b_1 \cdot 2 + b_0$ (bit $j$ contributes $2^j$), so the $2^D$ strings are exactly the numbers $0, 1, \dots, 2^D - 1$; Figure 1.2 shows each leaf’s value beneath its string. This weighting is what makes the binary encoding so convenient for arithmetic, where addition, subtraction, and multiplication all reduce to simple bit-level rules with carries. But as we will see in a moment, it is not the only way to encode a number.
Figure 1.2. Each leaf of the depth-$4$ tree labeled by its string and, beneath it, its decimal value in the usual binary encoding. The blue path highlights the string $\mathtt{0110}$.
Another encoding emerges from a simple puzzle about the tree. Let us start at the leftmost path $\mathtt{0000}$. We may hop from leaf to leaf, but each hop must flip exactly one bit of our current path. In other words, each hop relocates us to a leaf that shares all but one bit of our path. From $\mathtt{0000}$, for instance, a hop may take us to $\mathtt{0001}$, $\mathtt{0010}$, $\mathtt{0100}$, or $\mathtt{1000}$, but not to $\mathtt{0011}$. The puzzle is whether we can visit every leaf of the tree exactly once.
The answer is yes, and the solution is surprisingly simple: Figure 1.3 writes it out as a number line. Each number occupies one column, its decimal value on top, its binary encoding beneath, and a new string $g(k)$ at the bottom. Two observations. First, the bottom row is a reordering of the same sixteen strings, a permutation, hence invertible. The dashed lines pair each binary string with its new string. Second, consecutive strings in the bottom row differ in exactly one bit, and the figure marks the changed bit in red. The walk thus assigns each number a string, giving a second encoding of the numbers next to the binary one. This ordering is the (binary-reflected) Gray code, with $g(k)$ the $k$-th string of the bottom row. Figure 1.4 draws the same order as a walk on the tree. Starting from $\mathtt{0000}$, each hop flips one bit, and every leaf is visited exactly once.
Figure 1.3. The (binary-reflected) Gray code as a number line, at $D = 4$. Each column belongs to one number $k$: its decimal value, its binary encoding, and its Gray encoding $g(k)$. The bit that changes from each Gray string to the next is marked red, and the arrows trace the walk from $g(0)$ to $g(15)$.
Figure 1.4. The Gray walk on a depth-$4$ tree. Starting from the left-most path $\mathtt{0000}$ and hopping between leaves while flipping exactly one bit per hop visits every leaf exactly once, in the order $\mathtt{0000},$ $\mathtt{0001},$ $\mathtt{0011},$ $\mathtt{0010},$ $\mathtt{0110},$ $\mathtt{0111},$ $\mathtt{0101},$ $\mathtt{0100},$ $\mathtt{1100},$ $\mathtt{1101},$ $\mathtt{1111},$ $\mathtt{1110},$ $\mathtt{1010},$ $\mathtt{1011},$ $\mathtt{1001},$ $\mathtt{1000}$. The small number beneath each leaf is the leaf's position in the walk: following the arcs visits the indices in order $0, 1, 2, \dots$. Each hop flips exactly one bit, so consecutive labels always differ in a single position. The jump lengths follow the ruler pattern $1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1$.
Generating the walk π
Fortunately, converting a number between its binary encoding and its Gray code encoding is straightforward, with closed formulas in both directions. From now on we write $n_b$ for a number $n$ in its binary encoding and $n_g$ for the same number in its Gray code encoding, with $g$ converting between the two. Converting a number $n$ from binary to Gray: $$ g(n_b) := n_b \oplus (n_b \gg 1), \tag{1.1} $$
where $n_b \gg k$ shifts right by $k$ positions, discarding the lowest bits; converting a Gray encoding back to binary is the prefix XOR $$ g^{-1}(n_g) := n_g \oplus (n_g \gg 1) \oplus (n_g \gg 2) \oplus \cdots \oplus (n_g \gg (D{-}1)). \tag{1.2} $$
For example, $g(4) = \mathtt{0100} \oplus \mathtt{0010} = \mathtt{0110}$, matching position $4$ in Figure 1.3; and decoding $g^{-1}(\mathtt{0110}) = \mathtt{0110} \oplus \mathtt{0011} \oplus \mathtt{0001} = \mathtt{0100} = 4$ recovers the index.
With the conversion in hand, the walk of Figure 1.4 has a closed form. After $n$ hops from the starting path, we stand on $$ \mathrm{path}_n = \mathrm{path}_0 \oplus g(n_b) = \mathrm{path}_0 \oplus n_g, \tag{1.3} $$
the starting path XORed with the Gray encoding of the hop count (in the figures, $\mathrm{path}_0 = \mathtt{0000}$). The small number beneath each leaf in Figure 1.4 is the $n$ for which $\mathrm{path}_n$ is that leaf.
Distance as hops π
Figure 1.5(a) shows two paths on the depth-$4$ tree: $x = \mathtt{0110}$ and $y = \mathtt{1010}$. Let us analyse what happens if we XOR them: $$ x \oplus y = \mathtt{0110} \oplus \mathtt{1010} = \mathtt{1100}. $$
In the Hamming reading, the mask $\mathtt{1100}$ is just the set of differing positions, and $\mathrm{popcount}(\mathtt{1100}) = 2$ is the distance. But in the Graywalk metric, $\mathtt{1100}$ is a Gray code. It is $g(8)$, position $8$ on the number line of Figure 1.3. The inverse conversion from Equation (1.2) recovers that position, $$ g^{-1}(x \oplus y) = g^{-1}(\mathtt{1100}) = 8, $$
telling us that $x$ and $y$ sit eight hops apart on the walk. And indeed, starting from $x$ and flipping one bit at a time, $$ \mathtt{0110} ;\to; \mathtt{0111} ;\to; \mathtt{0101} ;\to; \mathtt{0100} ;\to; \mathtt{0000} ;\to; \mathtt{0001} ;\to; \mathtt{0011} ;\to; \mathtt{0010} ;\to; \mathtt{1010}, $$
we reach $y$ in exactly eight hops (Figure 1.5(b)). The XOR mask is not merely a difference indicator, it is a Gray-encoded hop count. This is the metric.
Figure 1.5. (a) Two paths on the depth-$4$ tree, $x = \mathtt{0110}$ (blue) and $y = \mathtt{1010}$ (red). Their XOR is the Gray string $\mathtt{1100} = g(8)$: the Graywalk distance is $8$ hops, while the Hamming distance is only $2$. The eight hops from $x$ to $y$, following the Gray walk from $x$, are drawn as arcs below the leaves. (b) The same eight hops as a chain; each arrow is annotated with the bit mask it flips.
Definition 1.1 (Graywalk metric). The Graywalk metric (Ramabaja distance) on $D$-bit strings is $$ d_G(x, y) := g^{-1}(x \oplus y). $$
It is norm-induced by $\lVert v \rVert := g^{-1}(v)$ and therefore XOR-translation invariant: $d_G(x, y) = \lVert x \oplus y \rVert$.
Two terms in this definition deserve a comment. A metric is norm-induced when the distance between two points is the size of their difference. Here the difference is the mask $x \oplus y$, and the size is its position on the number line, $\lVert v \rVert = g^{-1}(v)$. The Hamming distance is norm-induced in the same way, only with a different notion of size, the count of the mask. XOR-translation invariance then follows. Applying the same string $c$ to both points leaves the distance unchanged, $d_G(x \oplus c, y \oplus c) = g^{-1}(x \oplus c \oplus y \oplus c) = d_G(x, y)$, because the two copies of $c$ cancel. The distance between two strings thus depends only on how they differ, never on where they sit, and every point of the space sees the same geometry.
Notice, though, that while reaching $y$ from $x$ and reaching $x$ from $y$ take the same number of hops, the two walks are not the same. Recall from Equation (1.3) that starting from a path $p$, the $n$-th hop lands on $p \oplus n_g$, $n = 0, \dots, 2^D - 1$. We call this sequence the trail anchored at $p$. It is a Hamiltonian path of the $D$-cube: it visits every point exactly once, consecutive points differ in exactly one bit, and its two endpoints are $p$ and $p \oplus 2^{D-1}$. The index $n$ uses all $D$ bits, so it names exactly one point rather than a shell of candidates. It is a position one can read and write, with $2^D$ positions to move through where the Hamming scale has only $D{+}1$ values. Figure 1.6 shows the trails anchored at $x$ and at $y$. Each reaches the other in exactly $d_G(x, y) = 8$ hops, along its own route.
Figure 1.6. Two trails between the example pair $x = \mathtt{0110}$ (blue) and $y = \mathtt{1010}$ (red). The blue hops follow the trail anchored at $x$ and reach $y$ in exactly $d_G(x, y) = 8$ hops; the red hops follow the trail anchored at $y$ and reach $x$ in the same number of hops. The two trails share only their endpoints, $x$ and $y$. Each trail is calibrated to its own anchor: positions on the blue trail measure distance to $x$, positions on the red trail distance to $y$.
Incremental movement: pull and push π
But how do we actually pull and push points in this metric space? The important realization is that updating $x$ relative to $y$ must happen along $y$’s trail: $$ x’ = y \oplus g\bigl(g^{-1}(x \oplus y) - h_b\bigr). \tag{1.4} $$
A positive $h_b$ pulls $x$ toward $y$ by $h_b$ hops, valid for $0 \le h_b \le g^{-1}(x \oplus y)$. A negative $h_b$ pushes $x$ away from $y$ by $\lvert h_b \rvert$ hops, valid for $g^{-1}(x \oplus y) - (2^D - 1) \le h_b \le 0$.
Let us examine Figure 1.7 to form a more intuitive understanding of Equation (1.4). Recall from Figure 1.6 that the blue hops represent $x$’s trail to $y$ and the red hops $y$’s trail to $x$, with $x = \mathtt{0110}$, $y = \mathtt{1010}$, so $d_G(x, y) = g^{-1}(x \oplus y) = g^{-1}(\mathtt{1100}) = 8$. Say we want to move $x$ closer to $y$ by four hops, $h_b = 4$. If we walk along $x$’s trail instead, four hops from $x$ give $x’ = x \oplus g(8 - 4) = \mathtt{0000}$. In other words, performing four hops by walking along $x$’s trail did not actually pull $x$ closer to $y$. The distance to $y$ has grown to $d_G(x’, y) = g^{-1}(\mathtt{0000} \oplus \mathtt{1010}) = g^{-1}(\mathtt{1010}) = 12 > 8$. That is, four hops on $x$’s trail and four hops on $y$’s trail change the distance to $y$ differently. A hop count measures distance exactly only along the anchor’s own trail. For $x$ to move by four hops closer to $y$, we have to use $y$ as the anchor and compute $x’ = y \oplus g(8 - 4) = \mathtt{1010} \oplus g(8 - 4) = \mathtt{1010} \oplus \mathtt{0110} = \mathtt{1100}$ with $d_G(x’, y) = g^{-1}(\mathtt{1100} \oplus \mathtt{1010}) = g^{-1}(\mathtt{0110}) = 4$. We have moved $x$ by an exact, prescribed number of hops on a scale of $2^D$ units. This is fine-grained control the $(D{+}1)$-valued Hamming scale cannot offer.
Figure 1.7. Four hops from $x = \mathtt{0110}$ toward $y = \mathtt{1010}$ along two different trails. The blue hops follow $x$'s trail and reach $\mathtt{0000}$, where the distance to $y$ has grown to $12$, marked wrong. The red hops follow $y$'s trail and reach $\mathtt{1100}$, where the distance to $y$ has shrunk to $4$, marked correct.
The metric axioms π
For a distance to count as a metric, it must satisfy four rules. It must never be negative, it must be zero only for identical points, it must not depend on the direction of measurement, and it must satisfy the triangle inequality, the requirement that no detour is shorter than the direct walk. Let us check each of them for $d_G$, in order of increasing difficulty. Let us write $f = g^{-1}$ throughout, and recall that $f$ is a bijection of ${0, 1}^D$ onto ${0, \dots, 2^D - 1}$ with $f(0) = 0$.
The first three rules are easy to see. Non-negativity holds because $f$ takes non-negative integer values. Identity holds because $f$ is a bijection with $f(0) = 0$, so $d_G(x, y) = f(x \oplus y) = 0$ exactly when $x \oplus y = 0$, that is, when $x = y$. Symmetry holds because XOR commutes.
The fourth rule is the least obvious.
Theorem 1.2 (Triangle inequality). $d_G(x, z) \le d_G(x, y) + d_G(y, z)$.
Proof.
Since $d_G(x, z) = f(x \oplus z) = f\bigl((x \oplus y) \oplus (y \oplus z)\bigr)$, we only need to show that $f$ is subadditive, meaning $f(a \oplus b) \le f(a) + f(b)$, where $a = x \oplus y$ and $b = y \oplus z$. One structural fact about $f$ does most of the work. Take a string $w$ whose most significant bit $p$ is $1$, and let $w’$ be $w$ with that bit cleared. In the prefix XOR (recall Equation (1.2)), bit $p$ participates in the prefix of every bit below it, so bit $p$ of $f(w)$ is set and each lower bit of $f(w)$ is the complement of the corresponding bit of $f(w’)$. In the binary encoding, a $p$-bit number and its bitwise complement always sum to $2^p - 1$, so complementing a number means subtracting it from $2^p - 1$. Hence $f(w) = 2^p + (2^p - 1 - f(w’)) = 2^{p+1} - 1 - f(w’)$.
We induct on the string length $D$. The base $D = 1$ is trivial, since $f$ is the identity. For width $D$, let $\alpha$ be the most significant bit of $a$ and $a’$ the string $a$ with that bit cleared, and similarly $\beta$ and $b’$ for $b$. If $\alpha = \beta = 0$, the most significant bits were already $0$, so clearing them changes nothing and $a = a’$, $b = b’$, and $a \oplus b = a’ \oplus b’$. The claim for $(a, b)$ is then exactly the induction hypothesis for the shorter pair $(a’, b’)$, and we simply continue with $a’$ and $b’$ as the new $a$ and $b$. If $\alpha = \beta = 1$, the most significant bits cancel, so $f(a \oplus b) = f(a’ \oplus b’) \le 2^{D-1} - 1$, while the complement property just shown gives $f(a) + f(b) = 2^{D+1} - 2 - f(a’) - f(b’) \ge 2^D$. If $\alpha \neq \beta$ (without loss of generality $\alpha = 1$, since the other order follows by swapping $a$ and $b$), the most significant bit of $a \oplus b$ is $1$, so the complement property just shown applies to both $f(a)$ and $f(a \oplus b)$, while $f(b) = f(b’)$ since $\beta = 0$. Substituting into the claim, $$ f(a \oplus b) \le f(a) + f(b) \iff 2^D - 1 - f(a’ \oplus b’) \le 2^D - 1 - f(a’) + f(b’) \iff f(a’) \le f(a’ \oplus b’) + f(b’), $$
where the last step cancels $2^D - 1$ from both sides. The resulting inequality is the induction hypothesis applied to the pair $(a’ \oplus b’, b’)$, since $(a’ \oplus b’) \oplus b’ = a’$.
$\square$
For example, at $D = 4$, $a = \mathtt{1100}$ and $b = \mathtt{0100}$ give $f(a) = 8$ and $f(b) = 7$, while $f(a \oplus b) = f(\mathtt{1000}) = 15$.
Kademlia’s XOR metric π
While the Graywalk metric is energy-efficient in hardware (a walk that flips exactly one bit per hop minimizes switching activity, and switching activity is where digital energy goes, the very reason Gray codes were invented [1]), it becomes inconvenient as soon as arithmetic is involved. Gray-encoded numbers cannot be summed or subtracted, so every pull or push has to decode the mask to binary, perform the index arithmetic, and encode the result back, as in Equation (1.4). Any algorithm built on the Graywalk metric pays these two conversions per movement. What we would want instead is a metric with the same fine granularity and the same exact movement primitives, but whose arithmetic works directly on the mask.
Such a metric exists, and it is the closest relative of the Graywalk metric. The XOR metric of Kademlia [2], from now on simply the XOR metric, measures the distance between two $D$-bit strings as their XOR read as an unsigned integer, $d_K(x, y) := x \oplus y$. Since the decoder $g^{-1}$ is a $\mathrm{GF}(2)$-linear bijection, relabeling every point by its index on the origin’s trail is an isometry of the Graywalk space onto the XOR-metric space, so the two are the same space in different coordinates. The metric was introduced for peer discovery in distributed hash tables, where it gives the overlay a small-world shape (Figure 1.8). Each node keeps many contacts among its close peers and only a few among distant ones, a handful per level of the tree, made possible by the counting fact that exactly $2^i$ strings sit within distance $2^i$ of a node. A lookup clears the highest differing bit of the mask at every routing step, so the remaining distance at least halves per hop, and any target is reached in a logarithmic number of steps. It has served that role at scale for over two decades. To our knowledge it has never been used in a learning algorithm. This is surprising, since a fine-grained distance with exactly controllable movement is precisely what the training of embeddings asks for.
Figure 1.8. The small-world geometry of the XOR metric, seen from the anchor $x = \mathtt{0110}$ (blue path). Each dashed circle groups the leaves whose paths diverge from the anchor's path at the same level of the tree, and the circles double in size with every level, covering distances $1$, $2$β$3$, $4$β$7$, and $8$β$15$. A node keeps a handful of contacts in each circle, knowing its close neighborhood densely and the far reaches of the network sparsely. A lookup moves from one circle to the next smaller one, clearing the highest differing bit at each hop, so any target is reached in a logarithmic number of steps.
Figure 1.9. The XOR metric seen from the anchor $x = \mathtt{0110}$ (blue path). Beneath each leaf $\ell$ sits its distance $d_K(x, \ell)$, the mask $x \oplus \ell$ read as an unsigned integer. The arcs visit the leaves in order of increasing distance, $0, 1, \dots, 15$, starting at the anchor. Consecutive distances need not be neighbors; the hop from distance $7$ to distance $8$ crosses the entire tree.
Computing the XOR distance needs no conversion at all. For two strings $x$ and $y$, the mask $x \oplus y$ already is the distance, read as an unsigned integer. Figure 1.9 shows the whole space as seen from the anchor $x = \mathtt{0110}$, the blue path. Beneath each leaf $\ell$ sits its distance from the anchor, the mask $x \oplus \ell$ read as an unsigned integer. The arcs visit the leaves in order of increasing distance, from the anchor itself at distance $0$ to the farthest leaf at distance $15$.
Written out bit by bit, the XOR metric is a weighted Hamming distance, $$ d_K(x, y) = \sum_{i = 0}^{D - 1} 2^i , (x_i \oplus y_i), \tag{1.5} $$
where bit $i$ contributes with weight $2^i$. Equation (1.5) can be read as a weighted $\ell_1$ norm on ${0, 1}^D$. Distance is then a pure function of the mask $x \oplus y$, never of where the two points sit.
The Kademlia DHT, which introduced the XOR metric, never needed to pull or push points through this space. Fortunately, movement in the XOR metric is straightforward.
Incremental movement: pull and push π
Movement in the XOR metric is plain integer arithmetic. Writing $d_K(x, y)$ for the mask $x \oplus y$ read as an unsigned integer, updating $x$ relative to $y$ is a subtraction and a single XOR, $$ x’ = y \oplus \bigl(d_K(x, y) - h_b\bigr), \tag{1.6} $$
where the subtraction happens on integers and the result is XORed onto $y$ as a $D$-bit string. A positive $h_b$ pulls $x$ toward $y$ by $h_b$ units, valid for $0 \le h_b \le d_K(x, y)$. A negative $h_b$ pushes $x$ away from $y$ by $\lvert h_b \rvert$ units, valid for $d_K(x, y) - (2^D - 1) \le h_b \le 0$.
With the example pair of Figure 1.9, $x = \mathtt{0110}$ and $y = \mathtt{1010}$ sit at distance $12$. A pull of $h_b = 4$ gives $x’ = \mathtt{1010} \oplus 8 = \mathtt{0010}$, exactly four hops closer to $y$, computed with one subtraction and one XOR.
The metric axioms π
The weighted view makes the four metric axioms cheap to verify for the XOR metric.
Proposition 1.3. $d_K$ is a metric on the set of $D$-bit strings.
Proof.
The mask $x \oplus y$ is a non-negative integer, zero exactly when $x = y$, which gives non-negativity and identity, and XOR is commutative, which gives symmetry. For the triangle inequality, write the distance bit by bit, $d_K(x, y) = \sum_i 2^i (x_i \oplus y_i)$. For bits $a, b, c$ one has $a \oplus c \le a \oplus b + b \oplus c$, since either $a = c$ and the left side vanishes, or $a \neq c$ and $b$ differs from at least one of them, making the right side at least one. Multiplying by $2^i$ and summing over the coordinates gives $d_K(x, z) \le d_K(x, y) + d_K(y, z)$.
$\square$
The same statement for the Graywalk metric needed the top-bit recursion of Theorem 1.2. The difference is that the XOR metric is additive over the mask’s coordinates, while the Gray decoder mixes them.
References π
[1] Frank Gray. Pulse Code Communication. U.S. Patent 2,632,058, 1953. Filed November 13, 1947; issued March 17, 1953.
[2] Petar Maymounkov and David MaziΓ¨res. Kademlia: A Peer-to-Peer Information System Based on the XOR Metric. In Peer-to-Peer Systems: First International Workshop (IPTPS 2002), Lecture Notes in Computer Science, volume 2429, pages 53β65. Springer, 2002.
How to Cite This Work π
If you enjoyed this post, consider sharing it with others who might find it valuable. Curious about my work or interested in collaborating? Drop me a line. I’m always eager to connect with like-minded people.
If youβd like to reference this article in your own research or writing, please cite it as follows:
Lum Ramabaja (2026). Beyond Hamming: Graywalk and XOR-Based Metric Spaces.
Open Cybernetics Blog. https://lumramabaja.com/posts/beyond-hamming-graywalk-and-xor-based-metric-spaces/
You can also cite it in BibTeX format:
@article{ramabaja2026xormetricspaces,
title = {Beyond Hamming: Graywalk and XOR-Based Metric Spaces},
author = {Ramabaja, Lum},
year = {2026},
journal = {Open Cybernetics Blog},
url = {https://lumramabaja.com/posts/beyond-hamming-graywalk-and-xor-based-metric-spaces/}
}