The Hamming distance defines the number of bits that need to change in a binary value in order to produce another value. In this example we can enter a number of binary values:
Hamming distance |
Coding
The following defines the code:
import sys import commpy from bitstring import BitArray val1='0110,1111,0011,1010' if (len(sys.argv)>1): val1=sys.argv[1] def hamming2(str1, str2): if len(str1) != len(str2): print("Strings differ is size") return 0 a = int(str1, 2) b = int(str2, 2) count= 0 c = a ^ b while c: count += 1 c &= c - 1 return count a = val1.split(',') min=999 find1 = a[0] find2 = a[1] for x in range(0,len(a)-1): for y in range(x+1,len(a)): v1=BitArray(bin=a[x]) v2=BitArray(bin=a[y]) res=commpy.utilities.hamming_dist(v1,v2) if (res<min): min=res find1=a[x] find2=a[y] print("Hamming distance is ",min) print(" Minimum distance between ",find1," and ",find2)