Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace util
- {
- template <typename Ty, char N, char... Others>
- struct bin2dec {
- static_assert(N == '1' || N == '0', "Invaild binary number");
- static constexpr Ty value = ( static_cast<Ty>(N - '0') << sizeof...(Others) ) + bin2dec<Ty, Others...>::value;
- };
- template <typename Ty, char N>
- struct bin2dec<Ty, N> {
- static_assert(N == '1' || N == '0', "Invaild binary number");
- static constexpr Ty value = static_cast<Ty>(N - '0');
- };
- }
- template <char... Nums>
- constexpr int operator "" _b() {
- return util::bin2dec<int, Nums...>::value;
- }
- int main() {
- static_assert(0_b == 0, "");
- static_assert(101010_b == 42, "");
- static_assert(1111111_b == (1 << 7) - 1, "");
- static_assert(11111111111111111111111111111111_b == -1, "");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement