Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

196. Number of 1 Bits

EasyDay 58 of 60Bit Manipulation & Math

Given an unsigned 32-bit integer n, return how many of its bits are 1 (this count is also called the Hamming weight). In the tests n is passed as a plain non-negative number between 0 and 2^32 minus 1.

Example 1
Input: n = 11
Output: 3
11 is 1011 in binary, which has three 1 bits.
Example 2
Input: n = 4294967293
Output: 31
This is 11111111111111111111111111111101: every bit except one is set.
Constraints
  • 0 <= n <= 2^32 - 1 (an unsigned 32-bit value)
Hints
Frequently asked at: Apple, Microsoft, Qualcomm
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min