| Total Complexity | 4 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 16 | class Functions |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Checks if the number is a power of 2 |
||
| 20 | * |
||
| 21 | * @param int $x |
||
| 22 | * |
||
| 23 | * @return bool |
||
| 24 | */ |
||
| 25 | 11 | public static function isPowerOf2(int $x) : bool { |
|
| 26 | 11 | return ($x !== 0) && ($x & ($x - 1)) === 0; |
|
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Get the next power of 2 larger than input |
||
| 31 | * |
||
| 32 | * @param int $x |
||
| 33 | * |
||
| 34 | * @return int |
||
| 35 | */ |
||
| 36 | 9 | public static function nextPowerOf2(int $x) : int { |
|
| 37 | // Left bit shift by the bit length of the previous number |
||
| 38 | 9 | return 1 << strlen(decbin($x)); |
|
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get the previous power of 2 smaller or equal than input |
||
| 43 | * |
||
| 44 | * @param int $x |
||
| 45 | * |
||
| 46 | * @return int |
||
| 47 | */ |
||
| 48 | 9 | public static function previousPowerOf2(int $x) : int { |
|
| 51 | } |
||
| 52 | } |