NumberOfOneBits::hammingWeight()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
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