for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace TournamentGenerator\Helpers;
/**
* Static helper functions
*
* @package TournamentGenerator\Helpers
* @author Tomáš Vojík <[email protected]>
* @since 0.4
*/
class Functions
{
* Checks if the number is a power of 2
* @param int $x
* @return bool
public static function isPowerOf2(int $x) : bool {
return ($x !== 0) && ($x & ($x - 1)) === 0;
}
* Get the next power of 2 larger than input
* @return int
public static function nextPowerOf2(int $x) : int {
// Left bit shift by the bit length of the previous number
return 1 << strlen(decbin($x));
* Get the previous power of 2 smaller or equal than input
public static function previousPowerOf2(int $x) : int {
return 1 << (strlen(decbin($x)) - 1);