summaryrefslogtreecommitdiff
path: root/src/utils/binary.hpp
blob: 10807bc30f3f2a7c1b8e2c9179d944c7f3d38c77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef BINARY_INCLUDED
# define BINARY_INCLUDED

template<char FIRST, char... REST> struct binary
{
  static_assert(FIRST == '0' || FIRST == '1', "invalid binary digit" );
  enum { value = ((FIRST - '0') << sizeof...(REST)) + binary<REST...>::value };
};

template<> struct binary<'0'> { enum { value = 0 }; };
template<> struct binary<'1'> { enum { value = 1 }; };

template<char... LITERAL> inline
constexpr unsigned int operator "" _b() { return binary<LITERAL...>::value; }

#endif // BINARY_INCLUDED