Tulosba, если позволите, вот версия без рантайм циклов:
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| template <char first, char... bits>
struct bin_conv
{
enum
{
value = bin_conv<first>::value * (1 << sizeof...(bits)) + bin_conv<bits...>::value
};
};
template <char first>
struct bin_conv<first>
{
static_assert(first == '0' || first == '1', "not a binary number");
enum
{
value = first - '0'
};
};
template <char... digits>
constexpr int operator "" b()
{
return bin_conv<digits...>::value;
} |
|