How To Find If Two Numbers Are Consecutive Numbers In Gray Code Sequence
Solution 1:
Actually, several of the other answers seem wrong: it's true that two binary reflected Gray code neighbours differ by only one bit (I assume that by « the » Gray code sequence, you mean the original binary reflected Gray code sequence as described by Frank Gray). However, that does not mean that two Gray codes differing by one bit are neighbours (a => b
does not mean that b => a
). For example, the Gray codes 1000 and 1010 differ by only one bit but are not neighbours (1000 and 1010 are respectively 15 and 12 in decimal).
If you want to know whether two Gray codes a
and b
are neighbours, you have to check whether previous(a) = b OR next(a) = b
. For a given Gray code, you get one neighbour by flipping the rightmost bit and the other neighbour bit by flipping the bit at the left of the rightmost set bit. For the Gray code 1010, the neighbours are 1011 and 1110 (1000 is not one of them).
Whether you get the previous or the next neighbour by flipping one of these bits actually depends on the parity of the Gray code. However, since we want both neighbours, we don't have to check for parity. The following pseudo-code should tell you whether two Gray codes are neighbours (using C-like bitwise operations):
function are_gray_neighbours(a: gray, b: gray) -> booleanreturnb= a ^ 1ORb= a ^ ((a & -a) << 1)
end
Bit trick above: a & -a
isolates the rigthmost set bit in a number. We shift that bit by one position to the left to get the bit we need to flip.
Solution 2:
Assumptions: Inputs a and b are grey code sequences in binary reflected gray code. i.e a's and b's bit encoding is binary gray code representations.
#convert from greycode bits into regular binary bitsdefgTob(num): #num is binary graycode
mask = num >> 1while mask!=0:
num = num^mask
mask >>= 1return num; #num is converted #check if a and b are consecutive gray code encodingsdefareGrayNeighbors(a,b):
returnabs(gTob(a) - gTob(b)) == 1
Few Test cases:
- areGrayNeighbors(9,11) --> True (since (1001, 1011) differ in only one bit and are consecutive numbers in decimal representation)
- areGrayNeighbors(9,10) --> False
- areGrayNeighbors(14,10) --> True
References: method gTob() used above is from rodrigo in this post The neighbors in Gray code
Solution 3:
publicintcheckConsecutive2(int b1, int b2){
int x = (b1 ^ b2);
if((x & (x - 1)) !=0){
return0;
}else{
return1;
}
}
Solution 4:
If two numbers are in gray code sequence, they differ by one binary digit. i.e the exclusive OR on the two numbers returns a power of 2. So, find XOR and check if the result is a power of two.
This solution works well for the all the test cases written by CodeKaichu above. I would love to know if it fails in any cases.
publicbooleangrayCheck(int x, int y) {
intz= x^y;
return (z&z-1)==0;
}
Solution 5:
An obvious answer, but it works. Convert each gray code into its respective Binary form, subtract the two. If you answer is a binary equivalent of +1 or -1 then the two gray codes are adjacent.
This seems like an over kill, but when you're siting in an interview and don't know the correct method, this works. Also to optimize, one can check the single bit difference filter, so we don't waste time converting and subtracting numbers that we know for sure aren't adjacent.
Post a Comment for "How To Find If Two Numbers Are Consecutive Numbers In Gray Code Sequence"