P99

Bit fiddling of low order bits. More... Collaboration diagram for Bitfiddling:

## Macros

#define P99_LOW0SET(X)   ((X) | ((X) + 1))
set least significant zero bit More...

#define P99_LOW2(X)   ((X) & -(X))
extract the least significant bit that is non zero More...

#define P99_LOW2CLEAR(X)   ((X) & ((X) - 1))
clear the least significant bit that is non zero More...

#define P99_LOW2FILL(X)   ((X) | ((X) - 1))
fill all bits below least significant bit that is non zero More...

#define P99_LOW2MASK0(X)   (~(X) & ((X) - 1))
mask consisting of bits below and including the least significant bit that is non zero More...

#define P99_LOW2MASK1(X)   ((X) ^ ((X) - 1))
mask consisting of bits strictly below the least significant bit that is non zero More...

#define P99_LOW2SHIFT(X)   (P99_LIKELY(!!(X)) ? ((X) / ((X) & -(X))) : 0u)
shift number right such that the least significant bit that is non zero ends up at bit position 0 More...

mask consisting of bits strictly above the least significant bit that is non zero More...

mask consisting of bits above and including the least significant bit that is non zero More...

## Functions

uintmax_t p99_low0set (uintmax_t p00_x)
function equivalent to P99_LOW0SET More...

uintmax_t p99_low2 (uintmax_t p00_x)
function equivalent to P99_LOW2 More...

uintmax_t p99_low2clear (uintmax_t p00_x)
function equivalent to P99_LOW2CLEAR More...

uintmax_t p99_low2fill (uintmax_t p00_x)
function equivalent to P99_LOW2FILL More...

uintmax_t p99_low2shift (uintmax_t p00_x)
function equivalent to P99_LOW2SHIFT More...

uintmax_t p99_next_popcount (uintmax_t p00_x)
function that returns the next higher value that has exactly the same popcount as x. More...

## Detailed Description

Bit fiddling of low order bits.

The smart expression used in this group are taken as summarized in Christer Ericson's blog.

They work because of the special features of unsigned integer arithmetic that it is arithmetic modulo a power of `2`.

The notation of the macro and function names has some menomics:

• `low2` stands for the least significant bit resulting in the corresponding power of 2, something like `1 << bitposition`
• `mask` stands for a mask that is constructed from this, that is a sequence of bits that are all one. When `mask` comes after `low` as in `low2mask` it means that the bit mask follows that least significant bit. When `mask` comes before `low` as in `mask2low` it means that the bit mask precedes that least significant bit.
• The last digit of `0` or `1` indicates whether that least significant bit is cleared or left untouched.

Be careful when using the macros in this group:

• they may evaluate their arguments several times
• they must be only used with expressions that can be guaranteed to be unsigned

On the other hand prefer the macro when you known that you have compile time expressions. Then you may even use them as part of an `#``if` preprocessing conditional.