These functions provide some often needed bit manipulation capabilities. More...
Functions | |
| unsigned | bitcnt (unsigned x) |
| Number of one bits of a word. | |
| unsigned | ldzcnt (unsigned x) |
| Number of leading zero bits of a word. | |
| unsigned | trzcnt (unsigned x) |
| Number of trailing zero bits of a word. | |
| unsigned | bitrev (unsigned x) |
| Reverse bits in a word. | |
| unsigned | endian (unsigned x) |
| Change the endianness of a word. | |
These functions provide some often needed bit manipulation capabilities.
Although the actual functionality for each of these functions can be realised in a singe line of C code, the naive implementation involves looping. The implementations in the library use more efficient sequential algorithms and are written in assembly, thus they are rather fast.
To use these functions you have to include misc/bits.h.
| unsigned bitcnt | ( | unsigned | x | ) |
Number of one bits of a word.
The function counts the number of '1' bits in a 32-bit word and returns the count.
| x | The word in question |
| unsigned bitrev | ( | unsigned | x | ) |
Reverse bits in a word.
The function takes a 32-bit word, then swaps its LSB with its MSB, bit 1 with bit 30 and in general bitN with bit31-N for all N-s from 0 to 15.
| x | The word to reverse |
| unsigned endian | ( | unsigned | x | ) |
Change the endianness of a word.
The function changes a 32 bit word in little-endian byte order to a 32-bit word in big-endian byte order and vice versa. That is, if the function argument was 0x12345678 then the returned word will be 0x78563412.
| x | A word in some endiannes |
| unsigned ldzcnt | ( | unsigned | x | ) |
Number of leading zero bits of a word.
The function counts the number of consecutive 0 bits from the MSB of a 32-bit word. If the MSB is set, then the returned value is 0, if the word is 0, then the returned value is 32.
| x | The word in question |
| unsigned trzcnt | ( | unsigned | x | ) |
Number of trailing zero bits of a word.
The function counts the number of consecutive 0 bits from the LSB of a 32-bit word. If the LSB is set, then the returned value is 0, if the word is 0, then the returned value is 32.
| x | The word in question |
1.6.3