# Counting the number of bits set in a file

One interesting implementation of counting the number of bits set in a file. C ++ code with abundant use of STL. If everything is so good, then why is it on the ~~“Abnormal Programming”~~ blog (readers strongly recommended moving it to the C ++ blog) you ask? Now you will see :)

First, declare the class of the bit iterator:

Now let's calculate the number of set bits in the executable file:

It's funny, is it not? And to calculate the number of zero bits, you just need to call:

Instead:

The source code is posted here:~~codepad.org/S1TT56PR~~ (see link in UPDATE2).

Comrade gribozavr rightly remarked that it is more correct to use the CHAR_BIT constant instead of the number 8 in the code.

Comrade mark_ablov pointed out a flaw in the copy constructor, the code in the article was updated, and here is the corrected version of the code: http://codepad.org/knEKdNJX .

First, declare the class of the bit iterator:

Now let's calculate the number of set bits in the executable file:

It's funny, is it not? And to calculate the number of zero bits, you just need to call:

Instead:

The source code is posted here:

**UPDATE1:**Comrade gribozavr rightly remarked that it is more correct to use the CHAR_BIT constant instead of the number 8 in the code.

**UPDATE2:**Comrade mark_ablov pointed out a flaw in the copy constructor, the code in the article was updated, and here is the corrected version of the code: http://codepad.org/knEKdNJX .