NumberOfOneBits   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 2
b 0
f 0
dl 0
loc 22
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hammingWeight2() 0 9 2
A hammingWeight() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class NumberOfOneBits
8
{
9
    public static function hammingWeight(int $n): int
10
    {
11
        $cnt = 0;
12
        while ($n !== 0) {
13
            $n &= ($n - 1);
14
            $cnt++;
15
        }
16
17
        return $cnt;
18
    }
19
20
    public static function hammingWeight2(int $n): int
21
    {
22
        $cnt = 0;
23
        while ($n !== 0) {
24
            $cnt += $n & 1;
25
            $n >>= 1;
26
        }
27
28
        return $cnt;
29
    }
30
}
31