Count Set Bits
Learn Bit Manipulation DSA practice problem on Onlearn.
Difficulty: easy.
Topics: Count the number of set bits, Bitwise Operations, Loops, Time Complexity, Space Complexity, Big O Notation, bit manipulation, iterative algorithms, binary representation, brian kernighan's algorithm, lookup tables, Counting Set Bits (Brian Kernighan's Algorithm), Iterative Algorithms.
Count Set Bits Problem Statement Given a non negative integer N, count the number of set bits (i.e., bits with a value of '1') in its binary representation. Input Specification The input consists of a single line containing a non negative integer N. Output Specification Print a single integer, which is the count of set bits in the binary representation of N. Constraints 0 <= N <= 10^9 Sample Test Cases Sample Input 1: 5 Sample Output 1: 2 Explanation 1: The binary representation of 5 is 101. It has two set bits. Sample Input 2: 7 Sample Output 2: 3 Explanation 2: The binary representation of 7 is 111. It has three set bits. Sample Input 3: 0 Sample Output 3: 0 Explanation 3: The binary representation of 0 is 0. It has zero set bits.