Counting number of set bits (1) in a number (Brian Kernighan Algorithm)

Given an Integer and you have to count its set bits.


So here, Actually we have to find number of digits with value 1 in the given number when it is represented in its binary form.

i.e (5)10 = (0101)2

So number of count bits in 5 = 2

We have to just count number of 1's in given binary number.


We have explored two approaches:



Naive approach
Brian Kernighan Algorithm

Now how can we count ?


Naive Approach

As we know if we take AND of any number N with 1 then it returns the last or least signific...

 •  0 comments  •  flag
Share on Twitter
Published on August 16, 2020 14:30
No comments have been added yet.