Number of 1 Bits Problem & Solution

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).

See the number of 1 bits problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast")

static const int _=[](){std::ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
  int hammingWeight(uint32_t n) {
    return  __builtin_popcount(n);

Sample Search Queries

Many Paths. Follow Yours.