Privacy: no spam, one step unsubscribe. We'll only send high-signal dev content re DynamicBitset and other Boost libraries.
Boost.DynamicBitset is a C++ library that represents a set of bits similar to std::bitset but with the key difference that the number of bits can change at runtime. The library is header-only and requires C++11 or later.
Unlike std::bitset, which requires compile-time size specification through a template parameter, dynamic_bitset is dynamically resizable. This makes it ideal for applications where the bit set size is unknown until the program executes.
The library provides access to individual bit values through operator[] and supports all standard bitwise operators that work with built-in integers, such as operator&, operator|, operator^, and shift operators.
Recent updates have brought extensive modernization, including C++20 iterator support and constexpr functionality for almost all functions when compiled with C++20 or later.
Users can now choose the underlying container type for dynamic_bitset, enabling alternatives like boost::container::small_vector for small buffer optimization and more control over memory layout.
The library provides efficient bit-level storage and manipulation, with customizable block types for optimal memory usage and performance characteristics.
The bitwise operations correspond to mathematical set operations: operator& represents intersection, operator| represents union, and the library is specifically designed to represent subsets of finite sets where each bit indicates whether an element is in the subset.
The library offers extensive functionality including:
set(), reset(), flip(), test(), test_set() resize(), reserve(), shrink_to_fit(), push_back(), pop_back(), push_front(), pop_front() count(), size(), all(), any(), none(), empty() find_first(), find_next(), find_first_off(), find_next_off() is_subset_of(), is_proper_subset_of(), intersects() to_ulong(), to_string(), to_block_range()The library provides stream insertion and extraction operators for easy I/O, with proper locale support and exception handling.
The primary use case is representing subsets of finite sets, where each bit position indicates membership in the subset. This is valuable for:
Applications requiring efficient bit manipulation with runtime-determined sizes:
When you need to store large numbers of boolean flags efficiently:
A comprehensive modernization effort brought over 170 commits of improvements, including configurable underlying containers, C++20 iterator concepts, constexpr support throughout the library, removal of legacy workarounds for obsolete compilers, enhanced test coverage, and documentation migration to MrDocs and Antora.
Block: The unsigned integer type for internal storage (default: unsigned long) AllocatorOrContainer: Custom allocator or underlying container support (default: std::allocator<Block>)The library is straightforward to use:
#include <boost/dynamic_bitset.hpp>
// Create a bitset of 100 bits
boost::dynamic_bitset<> bits(100);
// Set some bits
bits.set(5);
bits[10] = true;
// Perform bitwise operations
boost::dynamic_bitset<> mask(100);
mask.set(); // Set all bits
auto result = bits & mask; // Intersection
// Check properties
if (bits.any()) {
std::cout << bits.count() << " bits are set\n";
}
c++, boost, boost-dynamic_bitset Boost.DynamicBitset provides a robust, flexible, and modern solution for runtime-sized bit manipulation in C++, combining the efficiency of low-level bit operations with the convenience of a high-level API.