Skip to main content

Count Set Bits

Learn Count Set Bits (Bit Manipulation, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Counting the Number of 1 Bits (Popcount) The simplest approach: check each of the 32 bits one at a time with (num >> i) & 1 and add up how many are 1. Correct, but does 32 iterations REGARDLESS of how many bits are actually set.

More lessons in Bit Manipulation