XOR distance is the secret sauce behind P2P routing
It turns out, once you stop treating it as just a bitwise operator and start treating it as a mathematical metric, it becomes the backbone of how distributed networks like Kademlia actually function.
The core logic
The definition is deceptively simple: to find the XOR distance between two IDs, you XOR their bits together and interpret the resulting bitstring as a standard integer. That integer is your distance. A larger number means the nodes are "far apart," and a smaller number means they are "close."

If we take two 4-bit IDs as a simplified example:
A = 1100
B = 1010
----
0110 (The XOR result)Reading 0110 as a decimal gives us 6. So, the distance between A and B is 6.

Why it actually qualifies as "distance"
In mathematics, you can't just slap the word "distance" on any arbitrary calculation. To be a formal metric, it has to satisfy specific properties, and XOR is surprisingly perfect for this:
- Identity of Indiscernibles:
distance(A, A) = 0. Since any bit XOR'd with itself is 0, the distance from a node to itself is always zero. - Symmetry:
A XOR Bis always equal toB XOR A. The direction of the comparison doesn't change the result. - Triangle Inequality:
distance(A, C) ≤ distance(A, B) + distance(B, C).

This third one is the heavy lifter for anyone building an LLM agent or a decentralized system. The triangle inequality guarantees that if you are routing a request and you keep jumping to nodes that have a smaller XOR distance to your target, you are mathematically guaranteed to converge on that target. You won't get stuck in an infinite loop or a local minimum.
The hierarchy of bits
This is where most people get tripped up. They confuse XOR distance with Hamming distance. Hamming distance just counts how many bits are different. XOR distance, however, treats the bits as a positional number.
In XOR distance, a bit flip in the most significant bit (the leftmost bit) is massively more "expensive" than a bit flip in the least significant bit. This creates a natural hierarchy that allows nodes to organize themselves into "buckets."
Here is a practical way to look at how these distances are categorized in a real-world implementation:
def xor_distance(a: int, b: int) -> int:
return a ^ b
def bucket_index(distance: int) -> int:
"""
Determines which 'bucket' a distance falls into.
This is essentially the index of the highest set bit,
which Kademlia uses to manage its routing table.
"""
return distance.bit_length() - 1 if distance else -1
# Example usage
A = 0b1100
B = 0b1010
C = 0b1101
print(f"Distance A to B: {xor_distance(A, B)}") # 6
print(f"Distance A to C: {xor_distance(A, C)}") # 1
print(f"Bucket for A-B: {bucket_index(xor_distance(A, B))}") # 2
print(f"Bucket for A-C: {bucket_index(xor_distance(A, C))}") # 0The bucket_index is the real magic. It tells you the scale of the distance. If a distance lands in a high bucket, the nodes share very little in common. If it's in a low bucket, they are almost identical in their bit-prefix. This structure allows a node to maintain a routing table that knows a little bit about everyone, but a lot about its immediate neighbors.
