2016-05-19

CSharp_BitCountAlgorithm


Bitcount algorithm in C#

In this article, we will learn:

  • Sparsh bitcount algorithm
  • Iterated bitcount algorithm
  • Precomputed bitcount algorithm
Bit counting is useful when using compact data structures in memory with bits. In this article, we will discuss various ways of counting total no of bits.

Sparsh bitcount algorithm:

This is a simple and fast algorithm that walks through all the bits that are set to one. It is static. It does not rely on saving state.

Output:

Sparsh_bitcount

Iterated bitcount algorithm:

This bit count is slow,simple and reliable.

Output:

Sparsh_bitcount

Precomputed bitcount algorithm:

This program demonstrates the use of a precomputed bitcount lookup table. The InitializeBitcounts method uses a logical method to precompute the bits in the table based on how the binary representation changes.

Output:

Sparsh_bitcount

SPECIAL THANKS TO CSHARPSTAR 

No comments:

HTMLCode

HTMLCode Content