function to calculate hamming distance