Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) GrayToInt

 

GrayToInt is a bit operation conversion code snippet to convert the integer value from a Gray code. To convert a Gray code to an integer, use IntToGray.

 

//From http://www.richelbilderbeek.nl/CppGrayToInt.htm
//Modified from Press et al., 2002, Numerical Recipies in C++,
//ISBN 0 521 75033 4
int GrayToInt(int i)
{
  int power = 1;
  while (1)
  {
    const int j = i >> power;
    i ^= j;
    if (j == 0 || power == 16) return i;
    power <<= 1;
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict