Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) IntToBitString

 

IntToBitString is a bit operation conversion code snippet to convert an integer to a std::string of zeroes and ones.

 

//From http://www.richelbilderbeek.nl/CppIntToBitString.htm
const std::string IntToBitString(int i)
{
  assert( i >= 0 && "Did not bother to supply this yet");
  std::string s = (i % 2 ? "1" : "0" );
  i>>=1;
  while (i > 0)
  {
    s = (i % 2 ? "1" : "0") + s;
    i>>=1;
  }
  return s;
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict