distanceExp
Returns n such that 2^n <= distance(n1, n2) < 2^(n+1). It is useful for finding which bucket a node belongs to. The value is the number of trailing bits after the shared bit prefix of n1 and n2; if the very first bits differ it is 159 (see the note below).
Faithful port of int distance_exp(...):
return std::max(159 - distance(n1, n2).count_leading_zeroes(), 0);Content copied to clipboard
libtorrent's own comment flags that returning 159 - clz (rather than 160 - clz) is "a little bit weird", but all of the routing-table code is tuned to this convention, so we reproduce it exactly. The off-by-one is part of the contract every caller (notably RoutingTable) relies on.