Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost::binary<>

Tags:

c++

boost

Is there anything in boost libraries like binary? For example I would like to write:

binary<10101> a;

I'm ashamed to admit that I've tried to find it (Google, Boost) but no results. They're mention something about binary_int<> but I couldn't find neither if it is available nor what header file shall I include;

Thanks for help.

like image 566
There is nothing we can do Avatar asked Dec 02 '25 10:12

There is nothing we can do


1 Answers

There is the BOOST_BINARY macro. used like this

int array[BOOST_BINARY(1010)];
  // equivalent to int array[012]; (decimal 10)

To go with your example:

template<int N> struct binary { static int const value = N; };
binary<BOOST_BINARY(10101)> a;

Once some compiler supports C++0x's user defined literals, you could write

template<char... digits>
struct conv2bin;

template<char high, char... digits>
struct conv2bin<high, digits...> {
    static_assert(high == '0' || high == '1', "no bin num!");
    static int const value = (high - '0') * (1 << sizeof...(digits)) + 
                             conv2bin<digits...>::value;
};

template<char high>
struct conv2bin<high> {
    static_assert(high == '0' || high == '1', "no bin num!");
    static int const value = (high - '0');
};

template<char... digits>
constexpr int operator "" _b() {
    return conv2bin<digits...>::value;
}

int array[1010_b];
like image 122
Johannes Schaub - litb Avatar answered Dec 04 '25 23:12

Johannes Schaub - litb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!