easyBit ManipulationPure DSA~12 min
Set Bit Permission Count
An authorization layer packs a user's granted permissions into a 32-bit mask, one bit per permission. An audit needs to report how many permissions each user holds — i.e. how many bits are set in the mask.
Problem
Given a non-negative 32-bit integer mask, return the number of set bits (its population count / Hamming weight).
Input
A non-negative integer mask (0 ≤ mask ≤ 2^32 − 1).
Output
A single integer — the count of 1-bits.
Constraints
- 0 ≤ mask ≤ 4,294,967,295
- Treat the value as an unsigned 32-bit pattern
Examples
Example 1
Input
mask = 11 (binary 1011)
Output
3
1011 has three 1-bits.
Example 2
Input
mask = 0
Output
0
No bits are set.