SBAA106A June 2020 – August 2021 ADS112C04 , ADS112U04 , ADS114S06 , ADS114S08 , ADS122C04 , ADS122U04 , ADS1235 , ADS1235-Q1 , ADS124S06 , ADS124S08 , ADS1259 , ADS1259-Q1 , ADS125H01 , ADS125H02 , ADS1260 , ADS1260-Q1 , ADS1261 , ADS1262 , ADS1263 , ADS127L01 , ADS131A02 , ADS131A04 , ADS131M04 , ADS131M06 , ADS131M08
A faster method of computing the number of set bits in the data is by using a lookup table. The following example uses data in a 4-bit lookup table. This requires eight passes to evaluate 32 bits. A larger lookup table reduces the number of passes, but increases the size of the table in memory.
uint32_t bitsArray[] = {
0, // 0
1, // 1
1, // 2
2, // 3
1, // 4
2, // 5
2, // 6
3, // 7
1, // 8
2, // 9
2, // 10
3, // 11
2, // 12
3, // 13
3, // 14
4, // 15
};
/**
* Computation of the number of set bits in a 32-bit value by lookup table.
*
* \details Counting of the number of bits set in a value using a loop and
* evaluating each nibble in the lookup table and then right shifting the remaining value by 4.
*
* \param uint32_t in of the value to be computed.
*
* \returns uint32_t numBits of the computation.
*/
uint32_t countBits (uint32_t in)
{
uint32_t numBits = 0;
while (in != 0)
{
numBits += bitsArray[in & 0x0F];
in >>= 4;
}
return numBits;
}