Similarity on same length bit streams
===

I want to calculate similarities between integers or more generally bit streams.

For example 0000,0001,0100,1000 are similar if we choose at most one bit different as metric.

Let our function be f(x,range) that x is the input number and range is values that we want to search in for similar numbers to x such that at most 2 bit different. One way is to implement f(x,range) is brute force that by xor'ing x with all numbers in range and counting the set bits. However, it is clearly inefficinet for massive datas. For example, if we want to calculate similar numbers in range 0-2^32  for all number in that set.This will take hours, maybe days. Is there an elegant solution that directly gives that numbers? It is very likely to be that there is no such solution, I think. It is obviously a similarity problem which is a big issue in many areas like data mining, computational geometry or other areas. However, here the datasets have same length, in other words, length of bit streams are same. So maybe here is a special solution for this problem that is why I am curious about it. Thank you very much.