...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The class templates in <boost/integer/integer_mask.hpp> provide bit masks for a certain bit position or a contiguousbit pack of a certain size. The types of the masking constants come from the integer type selection templates header.
#include <cstddef> // for std::size_t namespace boost { template <std::size_t Bit> struct high_bit_mask_t { typedef implementationdefinedtype least; typedef implementationdefinedtype fast; static const least high_bit = implementationdefined; static const fast high_bit_fast = implementationdefined; static const std::size_t bit_position = Bit; }; template <std::size_t Bits> struct low_bits_mask_t { typedef implementationdefinedtype least; typedef implementationdefinedtype fast; static const least sig_bits = implementationdefined; static const fast sig_bits_fast = implementationdefined; static const std::size_t bit_count = Bits; }; // Specializations for low_bits_mask_t exist for certain bit counts. } // namespace boost
The boost::high_bit_mask_t
class template provides constants
for bit masks representing the bit at a certain position. The masks are equivalent
to the value 2^{Bit}, where Bit
is the template parameter.
The bit position must be a nonnegative number from zero to Max,
where Max is one less than the number of bits supported by the largest unsigned
builtin integral type. The following table describes the members of an instantiation
of high_bit_mask_t
.
Table 2. Members of the boost::high_bit_mask_t
Class Template
Member 
Meaning 


The smallest, unsigned, builtin type that supports the given bit position. 

The easiesttomanipulate analog of 

A 

A 

The value of the template parameter, in case its needed from a renamed instantiation of the class template. 
The boost::low_bits_mask_t
class template provides constants
for bit masks equivalent to the value (2^{Bits}  1), where Bits
is the template parameter. The parameter Bits
must be
a nonnegative integer from zero to Max, where Max is
the number of bits supported by the largest, unsigned, builtin integral
type. The following table describes the members of low_bits_mask_t
.
Table 3. Members of the boost::low_bits_mask_t
Class Template
Member 
Meaning 


The smallest, unsigned builtin type that supports the given bit count. 

The easiesttomanipulate analog of 

A 

A 

The value of the template parameter, in case its needed from a renamed instantiation of the class template. 
When Bits
is the exact size of a builtin unsigned type,
the implementation has to change to prevent undefined behavior. Therefore,
there are specializations of low_bits_mask_t
at those
bit counts.
#include <boost/integer/integer_mask.hpp> //... int main() { typedef boost::high_bit_mask_t<29> mask1_type; typedef boost::low_bits_mask_t<15> mask2_type; mask1_type::least my_var1; mask2_type::fast my_var2; //... my_var1 = mask1_type::high_bit; my_var2 &= mask2_type::sig_bits_fast; //... }
The program integer_mask_test.cpp is a simplistic demonstration of the results from instantiating various examples of the bit mask class templates.
The class templates in this header are an extension of the integer type selection class templates. The new class templates provide the same sized types, but also convenient masks to use when extracting the highest or all the significant bits when the containing builtin type contains more bits. This prevents contamination of values by the higher, unused bits.
The author of the Boost bit mask class templates is Daryle Walker.