language-agnosticcomputer-sciencebit-manipulationbitbitcount

Why is it useful to count the number of bits?


I've seen the numerous questions about counting the number of set bits in an insert type of input, but why is it useful?

For those looking for algorithms about bit counting, look here:

  1. Counting common bits in a sequence of unsigned longs
  2. Fastest way to count number of bit transitions in an unsigned int
  3. How to count the number of set bits in a 32-bit integer?

Solution

  • You can regard a string of bits as a set, with a 1 representing membership of the set for the corresponding element. The bit count therefore gives you the population count of the set.

    Practical applications include compression, cryptography and error-correcting codes. See e.g. wikipedia.org/wiki/Hamming_weight and wikipedia.org/wiki/Hamming_distance.