Count the number of set bits (1s) in the binary representation of a non-negative integer. A set bit is a bit that has a value of 1 in the binary form of the number. For example, 5 in binary is 101, which has 2 set bits. Return 0 for input 0 and null for null input.
Examples:
Input: num = 5
Output: 2
Explanation: 5 in binary is 101, which has two 1s
Input: num = 7
Output: 3
Explanation: 7 in binary is 111, which has three 1s
Input: num = 8
Output: 1
Explanation: 8 in binary is 1000, which has one 1
Input: num = null
Output: null
Explanation: Null input returns null
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.