aboutsummaryrefslogtreecommitdiff
path: root/number_1bits.c
blob: c4a9e480c0f2e5d0917649bd5f4abce53145934b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 191. Number of 1 Bits
#include "leetcode.h"

/*
 * write a function that takes the binary representation of an unsigned integer
 * and returns number of '1' bits (also known as hamming weight). note that in
 * some languages, such as java, there is no unsigned integer type. in this
 * case, the input will be given as a signed integer type. it should not affect
 * your implementation as the integers binary representation is the same, wehter
 * it is signed or unsigned. in java, the compiler represents the signed
 * integers using 2's complement notation. therefore in example 3, the input
 * represents the signed integer -3
 */

int hammingWeight(uint32_t n) {
  int cnt = 0;
  while (n) {
    if (n & 1)
      cnt++;
    n >>= 1;
  }
  return cnt;
}

int main() {
  printf("%d\n", hammingWeight(00000000000000000000000000001011)); // expect: 3
  printf("%d\n", hammingWeight(00000000000000000000000010000000)); // expect: 3
  printf("%d\n", hammingWeight(11111111111111111111111111111101)); // expect: 31
}