PowerOfTwo   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isPowerOfTwo3() 0 3 4
A isPowerOfTwo() 0 3 2
A isPowerOfTwo2() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class PowerOfTwo
8
{
9
    public static function isPowerOfTwo(int $n): bool
10
    {
11
        return $n > 0 && (($n & ($n - 1)) === 0);
12
    }
13
14
    public static function isPowerOfTwo2(int $n): bool
15
    {
16
        if ($n <= 0) {
17
            return false;
18
        }
19
        while ($n % 2 === 0) {
20
            $n /= 2;
21
        }
22
23
        return $n === 1;
24
    }
25
26
    public static function isPowerOfTwo3(int $n): bool
27
    {
28
        return $n > 0 && ($n === 1 || ($n % 2 === 0 && self::isPowerOfTwo3($n / 2)));
29
    }
30
}
31