1270. Count Number of Set Bits (Hamming Weight)

EasyBit ManipulationBit ManipulationHamming WeightCounting

Given a non-negative integer n, return the number of 1-bits in its binary representation (its Hamming weight).

Input: A JSON object {"n": <non-negative integer>}.

Output: Return the count of set bits as an integer.

Examples

Example 1
Input: {"n":11}
Output: 3
Explanation: 1011 has three 1-bits -> 3.
Example 2
Input: {"n":128}
Output: 1
Explanation: 128 is 10000000 with a single 1-bit -> 1.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →