easyBit ManipulationPure DSA~11 min
Permission Mask Weight
A permission set is packed into a 32-bit mask where each set bit grants one capability. Count how many capabilities a given mask grants.
Problem
Given an unsigned 32-bit integer n, return the number of bits set to 1 in its binary representation (its Hamming weight).
Input
An unsigned 32-bit integer n (0 ≤ n < 2^32).
Output
An integer — the count of set bits.
Constraints
- Treat n as a 32-bit unsigned value
- Aim for time proportional to the number of set bits
Examples
Example 1
Input
n = 0b00000000000000000000000000001011
Output
3
Three bits are set.
Example 2
Input
n = 0b10000000000000000000000000000000
Output
1
Only the top bit is set.