Stack Bash - Master data structures and algorithms

Count bits

Medium
Given a decimal number n, write a function that returns the number of bits set to 1.
n = 12 # binary: 1100
Your function should return 2 since two of the four bits are set.

Try it first

Solution

6 Essential Primitives Coding Interview Problems

Master Primitives by trying the coding challenges below.
  1. 1.Swap bitsEasy
  2. 2.Power of twoEasy
  3. 3.Count bitsMedium
  4. 4.Reverse bitsMedium
  5. 5.Find duplicateMedium
  6. 6.Bitwise additionHard

Want to confidently pass your next coding interview?

Stack Bash helps new and veteran software engineers master data structures and algorithms for technical interviews.