P99
Macros | Functions

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...
 
#define P99_MASK2LOW0(X)   ((X) ^ -(X))
 mask consisting of bits strictly above the least significant bit that is non zero More...
 
#define P99_MASK2LOW1(X)   ((X) | -(X))
 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_low2mask0 (uintmax_t p00_x)
 function equivalent to P99_LOW2MASK0 More...
 
uintmax_t p99_low2mask1 (uintmax_t p00_x)
 function equivalent to P99_LOW2MASK1 More...
 
uintmax_t p99_low2shift (uintmax_t p00_x)
 function equivalent to P99_LOW2SHIFT More...
 
uintmax_t p99_mask2low0 (uintmax_t p00_x)
 function equivalent to P99_MASK2LOW0 More...
 
uintmax_t p99_mask2low1 (uintmax_t p00_x)
 function equivalent to P99_MASK2LOW1 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:

Be careful when using the macros in this group:

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.