|
BDE 4.14.0 Production release
|
Provide efficient bit-manipulation of uint32_t/uint64_t values.
uint32_t and uint64_t bit-level operationsThis component provides a utility struct, bdlb::BitUtil, that serves as a namespace for a collection of efficient, bit-level procedures on 32- and 64-bit unsigned integer types. In particular, BitUtil supplies single bit manipulation, bit counting, and mathematical functions that can be optimized with bitwise operations.
This component is meant to interoperate cleanly both with fundamental types, as well as common sized integer type aliases like bsl::uin64_t and bslsl::Types::UInt64. An overload set consisting of the fundamental integer types unsigned int, unsigned long, and unsigned long long is used to minimize warnings and avoid ambiguity that may arise when dealing with explicitly sized types that may alias to different fundamental types on different platforms.
Some of the methods provided in BitUtil have other common names. Below is a list of mappings from the name used in BitUtil to these related function names:
The following usage examples illustrate how some of the methods provided by this component are used. Note that, in all of these examples, the low-order bit is considered bit 0 and resides on the right edge of the bit string.
First, we use withBitSet to demonstrate the ordering of bits:
Then, we count the number of set bits in a value with numBitsSet:
Finally, we use numLeadingUnsetBits to determine the number of unset bits with a higher index than the first set bit: