Count the number of set bits (1s) in the binary representation of an integer (Brian Kernighan's algorithm).
Difficulty: Medium
Phase: 6 · Mixed Logical Challenges
XP Reward: 20 XP
Topics: Mixed Logical Challenges, Medium
Supported Languages: Python, JavaScript, C, C++, Java
Problem Description
Given non-negative integer N, count number of set bits (1s) in binary using Brian Kernighan's algorithm. Input Format: Read input from standard input (stdin). Output Format: Print the result to standard output (stdout).
Constraints
- -10^9 <= N <= 10^9
Examples
Example 1
Input: 15
Output: 4
Explanation: Sample execution for input 15