Is there any algorithm that I can use to find the neighbors in Gray code?
For small numbers is just fine to write the entire table, but if I have a number like 010 110 is a bit to much to write the entire grey code table with 6 numbers.
Is there any algorithm that I can use to find the neighbors in Gray code?
For small numbers is just fine to write the entire table, but if I have a number like 010 110 is a bit to much to write the entire grey code table with 6 numbers.
By definition, any perturbation with a single bit change is a valid adjacent Gray code. The problem is that for a six bit value there are six possible results and only two of them can be the correct one in any single coding.
The non-determinism gets worse as you increase the word size.
Copied shamelessly from Wikipedia:
And now, the requested code, using the mask to limit the number of bits to 6:
The fastest solution is to convert the gray code to regular binary, get the next value and convert the value back to gray code. Those operations are probably the fastest and the simplest you could get.
Otherwise, you can use the following operation:
As you can see, you have to know the parity of the gray code in order to know which computation to apply. The parity of a regular gray code is the same as the parity of its number of set bits. Hence the following formula to compute
is_gray_odd
:The function
previous_gray
will be the same as the functionnext_gray
, except that you have to reverse the condition. Anyway, the back and forth conversion to regular may eventually be faster.EDIT: if you are using GCC or Clang, you can use the compiler intrinsic
__builtin_parity
to compute the parity of a gray code (and possibly check for the existence of__GNUC__
and__clang__
to remain cross-platform):If you do so, computing the next/previous gray code can be faster than converting the gray code back and forth to binary on some architectures. Anyway, if you want speed, you better benchmark.
EDIT 2: If you only need the two neighbours and you don't care about which is the previous one and which is the next one, they you don't even care about the parity, you can get both of them like this: