I want to calculate all possbie hamming neighbours from a given byte with a maximum hamming distance.
For a hamming distance of 1 I have created this function:
public static ArrayList<Byte> hammingNeighbours(byte input, int maxDistance){
ArrayList<Byte> neighbours = new ArrayList<>();
neighbours.add(input);
byte value;;
byte mask = 1;
for (int i = 0; i < 8; i++) {
value = (byte) (input ^mask);
neighbours.add(value);
mask = (byte) (mask << 1);
}
return neighbours;
}
But how to add neighbours with a distance > 1? can someone help me to solve this problem?
best regards
As I said in the comments, all the byte
which do not have a distance == 1 or 0 would be valid. But if you want an algorithm which will give you all the bytes which are at most maxDist
away, you could do a recursive method as such:
public static void getNeighbours(ArrayList<Byte> nbrs, byte input, int bit, int maxDist) {
if(maxDist == 0 || bit == 8) {
nbrs.add(input);
} else {
getNeighbours(nbrs, (byte) (input^(1<<bit)), bit+1, maxDist-1);
getNeighbours(nbrs, input, bit+1, maxDist);
}
}
If you only want the bytes which are exactly maxDist
away, then only add if(maxDist == 0)
and terminate the branch if(bit == 8)