2進数 2進定数をテンプレートを使用して記述する方法
ttp://cppemb.blog17.fc2.com/blog-entry-42.html
転載
int x = B(0101); /* 0x05 */
template <int Bit> struct bit; template <> struct bit<0> { static const int value = 0; }; template <> struct bit<1> { static const int value = 1; }; template <unsigned long N> struct binary { static const unsigned long value = bit<N & 7>::value | (binary<N >> 3>::value << 1); }; template <> struct binary<0> { static const unsigned long value = 0; }; #define B(x) (binary<0##x>::value)
テンプレート覚えたてで、なにが起こっているのかわからない
→ 101を8進数としてみる。
101(8進数) = 65(10進数)
65(10進数) = 001 000 001(2進数)
下位3ビットを再帰的に切り取っていく。