Function Repository Resource:

InverseGrayCode

Source Notebook

Find the integer corresponding to a given Gray code

Contributed by: Jan Mangaldan

ResourceFunction["InverseGrayCode"][{b1,b2,}]

gives the integer corresponding to the Gray code represented by the bits bi.

Examples

Basic Examples (1) 

Find the integer corresponding to a given Gray code:

In[1]:=
ResourceFunction["InverseGrayCode"][{1, 0, 0, 1}]
Out[1]=

Applications (2) 

Use InverseGrayCode to generate the Thue–Morse sequence:

In[2]:=
BitAnd[ResourceFunction["InverseGrayCode"] /@ IntegerDigits[Range[25], 2], 1]
Out[2]=

Compare with the built-in ThueMorse:

In[3]:=
ThueMorse[Range[25]]
Out[3]=

Use InverseGrayCode to generate the Rudin–Shapiro sequence:

In[4]:=
With[{r = BitAnd[Range[25], BitShiftRight[Range[25]]]}, 1 - 2 BitAnd[
    ResourceFunction["InverseGrayCode"] /@ IntegerDigits[r, 2], 1]]
Out[4]=

Compare with the built-in RudinShapiro:

In[5]:=
RudinShapiro[Range[25]]
Out[5]=

Properties and Relations (1) 

InverseGrayCode is the inverse of the resource function GrayCode:

In[6]:=
With[{n = 25}, ResourceFunction["InverseGrayCode"] /@ ResourceFunction["GrayCode"][Range[n]] === Range[n]]
Out[6]=

Version History

  • 1.0.0 – 28 June 2021

Related Resources

License Information