Passed
Push — master ( 795926...7012fc )
by Tomáš
11:11
created

Functions   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 35
ccs 6
cts 6
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A previousPowerOf2() 0 3 1
A nextPowerOf2() 0 3 1
A isPowerOf2() 0 2 2
1
<?php
2
3
4
namespace TournamentGenerator\Helpers;
5
6
7
/**
8
 * Static helper functions
9
 *
10
 * @package TournamentGenerator\Helpers
11
 *
12
 * @author  Tomáš Vojík <[email protected]>
13
 *
14
 * @since   0.4
15
 */
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 {
49
		// Left bit shift by the bit length of the previous number
50 9
		return 1 << (strlen(decbin($x)) - 1);
51
	}
52
}