find_first_one
count_trailing_ones
count_leading_ones
find_first_zero
count_trailing_zeros
count_leading_zeros
See Find First Set.
The first 1 bit is often of interest.
This becomes more interesting if the number is signed, in which case, we may be looking for the arithmetic first and last 1 bits.
Implement these functions.
std::size_t first_one( unsigned int value ); std::size_t last_one( unsigned int value ); std::size_t first_one( int value ); std::size_t last_one( int value ); std::size_t arithmetic_first_one( int value ); std::size_t arithmetic_last_one( int value );
For a templated data type, you will have to recall that the size of the type can be found using the sizeof operator.
template <typename T> std::size_t first_one( T value ); template <typename T> std::size_t last_one( T value ); template <typename T> std::size_t arithmetic_first_one( T value ); template <typename T> std::size_t arithmetic_last_one( T value );
If the type is unsigned, the arithmetic variants are identical to the descriptions given above.
To tell whether or not the type is signed, you can access the member variable std::is_signed<T>::value of the structure defined in the type_traits library.