# Bit manipulation functions
[Miscellaneous functions]

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.

## Detailed Description

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`.

## Function Documentation

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.

Parameters:

  | x | The word in question

Returns:
  The number of 1 bits in the word

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.

Parameters:

  | x | The word to reverse

Returns:
  The reversed word

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`.

Parameters:

  | x | A word in some endiannes

Returns:
  The word in the other endianness

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.

Parameters:

  | x | The word in question

Returns:
  The number of leading 0 bits

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.

Parameters:

  | x | The word in question

Returns:
  The number of trailing 0 bits