Created
July 24, 2017 19:07
-
-
Save Staticity/9163bd9a8ce9449819eddb0b7142f4d9 to your computer and use it in GitHub Desktop.
Templated version of a `ToBinaryString` function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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