Test Failed
Push — master ( 6d15a1...d2aec8 )
by Jinyun
02:07
created

ReverseBits::reverseBits2()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class ReverseBits
8
{
9
    public static function reverseBits(int $n): int
10
    {
11
        if ($n === 0) {
12
            return 0;
13
        }
14
        $ans = 0;
15
        for ($i = 0; $i < 32; $i++) {
16
            $ans <<= 1;
17
            $ans += $n & 1;
18
            $n >>= 1;
19
        }
20
21
        return $ans;
22
    }
23
24
    public static function reverseBits2(int $n): int
25
    {
26
        if ($n === 0) {
27
            return 0;
28
        }
29
        $ans = 0;
30
        for ($i = 0; $i < 32; $i++) {
31
            $ans = ($ans << 1) ^ ($n & 1);
32
            $n >>= 1;
33
        }
34
35
        return $ans;
36
    }
37
}
38