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!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow 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 Feb 2026
Contest runs February 1 - 28. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.