二进制的语法
int i = 0b101010; // binary
int i = 052; // octal
int i = 42; // decimal
int i = 0x2a; // hexadecimal
// unsigned long long constructor
std::bitset<8> (42);
// string constructor
std::string bits = "110010";
std::bitset<8> binary(bits); // [0,0,1,1,0,0,1,0]
// string constructor using custom zero/one digits
std::string bacon = "ABABB";
std::bitset<5> m(bacon, 0, std::string::npos, ‘A‘, ‘B‘); // [0,1,0,1,1]
int value1 = BOOST_BINARY( 100 111000 01 1 110 );
unsigned long value2 = BOOST_BINARY_UL( 100 001 ); // unsigned long
long long value3 = BOOST_BINARY_LL( 11 000 ); // long long if supported
template<unsigned long long N>
struct binary
{
enum { value = (N%10) + binary<N/10>::value*2 };
};
template<>
struct binary
{
enum { value = 0 };
}
// need to enable C++11 flag
template<unsigned long long>
struct binary
{
constexpr static int value = binary<N/8>::value + N%8;
};
template<>
struct binary<0>
{
constexpr static int value = 0;
}
int operator "" _B(int i);
static_assert( 101010_B == 42);
原文:http://www.cnblogs.com/Martinium/p/binary_literal.html