Skip to content

Instantly share code, notes, and snippets.

@Staticity
Created July 24, 2017 19:07
Show Gist options
  • Save Staticity/9163bd9a8ce9449819eddb0b7142f4d9 to your computer and use it in GitHub Desktop.
Save Staticity/9163bd9a8ce9449819eddb0b7142f4d9 to your computer and use it in GitHub Desktop.
Templated version of a `ToBinaryString` function
#include <iostream>
#include <string>
template <int N>
struct ToBinaryString
{
inline static std::string result()
{
return ToBinaryString<N / 2>::result() + ((N & 1) == 0 ? "0" : "1");
}
};
template <>
struct ToBinaryString<0>
{
inline static std::string result()
{
return "0";
}
};
template <>
struct ToBinaryString<1>
{
inline static std::string result()
{
return "1";
}
};
int main()
{
std::cout << ToBinaryString<85>::result() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment