Problem
Write a function that takes a positive integer and returns the number of set bits (1s) in its binary representation.
References
Difficulty
🟢 Easy
Companies
Amazon, Sumo Logic, Paytm
Notes
Language: Java
Trick: n & (n-1) clears the lowest set bit. Count how many times you can do this before n becomes 0.
Problem
Write a function that takes a positive integer and returns the number of set bits (1s) in its binary representation.
References
Difficulty
🟢 Easy
Companies
Amazon, Sumo Logic, Paytm
Notes
Language: Java
Trick: n & (n-1) clears the lowest set bit. Count how many times you can do this before n becomes 0.