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
One method of computing the number of bits set in the data is to evaluate each bit one at a time. The following method requires 32 passes through the data to evaluate each of the 32 bits.
/**
* Computation of the number of set bits in a 32-bit value.
*
* \details Counting of the number of bits set in a value using a loop and
* evaluating each least significant bit and then right shifting the remaining value by 1.
*
* \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)
{
if (in & 0x01) numBits++;
in >>= 1;
}
return numBits;
}