| Home | Libraries | People | FAQ | More

### Binary Integer Literals

#### Introduction

The macro `BOOST_BINARY` is used for the representation of binary literals. It takes as an argument a binary number arranged as an arbitrary amount of 1s and 0s in groupings of length 1 to 8, with groups separated by spaces. The macro serves as a replacement for binary integer literals, adopted in C++14.

The type of the literal yielded is determined by the same rules as those of hex and octal literals. By implementation, this macro expands directly to an octal literal during preprocessing, so there is no overhead at runtime and the result is usable in any place that an octal literal would be.

In order to directly support binary literals with suffixes, additional macros of the form `BOOST_BINARY_XXX` are also provided, where `XXX` is a standard integer suffix in all capital letters.

In addition, LL and ULL suffixes may be used for representing `long long` and `unsigned long long` types in compilers which provide them as an extension.

The `BOOST_BINARY` family of macros resides in the header <boost/utility/binary.hpp>.

#### Example

    void foo( int );

    void foo( unsigned long );

    void bar()
    {
      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

      assert(    BOOST_BINARY( 10010 )
              &  BOOST_BINARY( 11000 )
              == BOOST_BINARY( 10000 )
            );

      foo( BOOST_BINARY( 1010 ) ); // calls the first foo

      foo( BOOST_BINARY_LU( 1010 ) ); // calls the second foo
    }

#### Reference

##### Header <boost/utility/binary.hpp>

    BOOST_BINARY(bit_groupings)
    BOOST_BINARY_U(bit_groupings)
    BOOST_BINARY_L(bit_groupings)
    BOOST_BINARY_UL(bit_groupings)
    BOOST_BINARY_LU(bit_groupings)
    BOOST_BINARY_LL(bit_groupings)
    BOOST_BINARY_ULL(bit_groupings)
    BOOST_BINARY_LLU(bit_groupings)
    BOOST_SUFFIXED_BINARY_LITERAL(bit_groupings, suffix)
    BOOST_SUFFIXED_BINARY_LITERAL_D(d, bit_groupings, suffix)
    BOOST_BINARY_LITERAL_D(d, bit_groupings)

#### Acknowledgments

Contributed by Matt Calabrese.