Skip to content

Commit

Permalink
add hasAny. refactor parameters to be more meaningful
Browse files Browse the repository at this point in the history
  • Loading branch information
TomK committed Feb 26, 2016
1 parent fc19154 commit 13fed2d
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 68 deletions.
70 changes: 45 additions & 25 deletions src/BitWise.php
Original file line number Diff line number Diff line change
@@ -1,113 +1,133 @@
<?php
namespace Packaged\Helpers;

class BitWise
class BitWise implements BitWiseInterface
{
/**
* Check to see if an integer is a single bit, or a combination
*
* @param int $bit Bit to check
* @param int $value Bit to check
*
* @return bool
*/
public static function isSingleBit($bit)
public static function isSingleBit($value)
{
if(extension_loaded('gmp'))
{
return BitWiseGmp::isSingleBit($bit);
return BitWiseGmp::isSingleBit($value);
}
else
{
return BitWiseInt::isSingleBit($bit);
return BitWiseInt::isSingleBit($value);
}
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return string
*/
public static function remove($mask, $bit)
public static function remove($value, $mask)
{
if(extension_loaded('gmp'))
{
return BitWiseGmp::remove($mask, $bit);
return BitWiseGmp::remove($value, $mask);
}
else
{
return BitWiseInt::remove($mask, $bit);
return BitWiseInt::remove($value, $mask);
}
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return string
*/
public static function add($mask, $bit)
public static function add($value, $mask)
{
if(extension_loaded('gmp'))
{
return BitWiseGmp::add($mask, $bit);
return BitWiseGmp::add($value, $mask);
}
else
{
return BitWiseInt::add($mask, $bit);
return BitWiseInt::add($value, $mask);
}
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return string
*/
public static function toggle($mask, $bit)
public static function toggle($value, $mask)
{
if(extension_loaded('gmp'))
{
return BitWiseGmp::toggle($mask, $bit);
return BitWiseGmp::toggle($value, $mask);
}
else
{
return BitWiseInt::toggle($mask, $bit);
return BitWiseInt::toggle($value, $mask);
}
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return bool
* @internal param bool $strict
*
*/
public static function has($mask, $bit)
public static function has($value, $mask)
{
if(extension_loaded('gmp'))
{
return BitWiseGmp::has($mask, $bit);
return BitWiseGmp::has($value, $mask);
}
else
{
return BitWiseInt::has($mask, $bit);
return BitWiseInt::has($value, $mask);
}
}

/**
* @param $mask
* @param $value
* @param $mask
*
* @return bool
*/
public static function hasAny($value, $mask)
{
if(extension_loaded('gmp'))
{
return BitWiseGmp::hasAny($value, $mask);
}
else
{
return BitWiseInt::hasAny($value, $mask);
}
}

/**
* @param $value
*
* @return string
*/
public static function getBits($mask)
public static function getBits($value)
{
if(extension_loaded('gmp'))
{
return BitWiseGmp::getBits($mask);
return BitWiseGmp::getBits($value);
}
else
{
return BitWiseInt::getBits($mask);
return BitWiseInt::getBits($value);
}
}

Expand Down
55 changes: 33 additions & 22 deletions src/BitWiseGmp.php
Original file line number Diff line number Diff line change
@@ -1,79 +1,90 @@
<?php
namespace Packaged\Helpers;

class BitWiseGmp
class BitWiseGmp implements BitWiseInterface
{
/**
* Check to see if an integer is a single bit, or a combination
*
* @param mixed $bit Bit to check
* @param mixed $value Bit to check
*
* @return bool
*/
public static function isSingleBit($bit)
public static function isSingleBit($value)
{
return
(gmp_cmp($bit, 1) === 0)
|| ($bit > 0
&& gmp_cmp(gmp_mod($bit, 2), 0) === 0
&& gmp_cmp(gmp_and($bit, gmp_sub($bit, 1)), 0) === 0);
(gmp_cmp($value, 1) === 0)
|| ($value > 0
&& gmp_cmp(gmp_mod($value, 2), 0) === 0
&& gmp_cmp(gmp_and($value, gmp_sub($value, 1)), 0) === 0);
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return string
*/
public static function remove($mask, $bit)
public static function remove($value, $mask)
{
return gmp_strval(gmp_and($mask, gmp_com($bit)));
return gmp_strval(gmp_and($value, gmp_com($mask)));
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return string
*/
public static function add($mask, $bit)
public static function add($value, $mask)
{
return gmp_strval(gmp_or($mask, $bit));
return gmp_strval(gmp_or($value, $mask));
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return string
*/
public static function toggle($mask, $bit)
public static function toggle($value, $mask)
{
return gmp_strval(gmp_xor($mask, $bit));
return gmp_strval(gmp_xor($value, $mask));
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return bool
*/
public static function has($mask, $bit)
public static function has($value, $mask)
{
return gmp_cmp(gmp_and($mask, $bit), $bit) === 0;
return gmp_cmp(gmp_and($value, $mask), $mask) === 0;
}

/**
* @param $value
* @param $mask
*
* @return bool
*/
public static function hasAny($value, $mask)
{
return gmp_cmp(gmp_and($value, $mask), 0) !== 0;
}

/**
* @param $value
*
* @return string
*/
public static function getBits($mask)
public static function getBits($value)
{
$bits = [];
for($i = gmp_init(1); gmp_cmp($mask, $i) >= 0; $i = gmp_mul($i, 2))
for($i = gmp_init(1); gmp_cmp($value, $i) >= 0; $i = gmp_mul($i, 2))
{
if(static::has($mask, $i))
if(static::has($value, $i))
{
$bits[] = gmp_strval($i);
}
Expand Down
53 changes: 32 additions & 21 deletions src/BitWiseInt.php
Original file line number Diff line number Diff line change
@@ -1,78 +1,89 @@
<?php
namespace Packaged\Helpers;

class BitWiseInt
class BitWiseInt implements BitWiseInterface
{
/**
* Check to see if an integer is a single bit, or a combination
*
* @param int $bit Bit to check
* @param int $value Bit to check
*
* @return bool
*/
public static function isSingleBit($bit)
public static function isSingleBit($value)
{
$bit = (int)$bit;
$value = (int)$value;
return
($bit === 1)
|| ($bit > 0 && (($bit % 2) == 0) && (($bit & ($bit - 1)) == 0));
($value === 1)
|| ($value > 0 && (($value % 2) == 0) && (($value & ($value - 1)) == 0));
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return string
*/
public static function remove($mask, $bit)
public static function remove($value, $mask)
{
return (int)$mask & (~(int)$bit);
return (int)$value & (~(int)$mask);
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return string
*/
public static function add($mask, $bit)
public static function add($value, $mask)
{
return (int)$mask | (int)$bit;
return (int)$value | (int)$mask;
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return string
*/
public static function toggle($mask, $bit)
public static function toggle($value, $mask)
{
return (int)$mask ^ (int)$bit;
return (int)$value ^ (int)$mask;
}

/**
* @param $value
* @param $mask
* @param $bit
*
* @return bool
*/
public static function has($mask, $bit)
public static function has($value, $mask)
{
return ((int)$mask & (int)$bit) === (int)$bit;
return ((int)$value & (int)$mask) === (int)$mask;
}

/**
* @param $value
* @param $mask
*
* @return bool
*/
public static function hasAny($value, $mask)
{
return ((int)$value & (int)$mask) !== 0;
}

/**
* @param $value
*
* @return string
*/
public static function getBits($mask)
public static function getBits($value)
{
$bits = [];
for($i = 1; $i <= $mask; $i *= 2)
for($i = 1; $i <= $value; $i *= 2)
{
if(static::has($mask, $i))
if(static::has($value, $i))
{
$bits[] = $i;
}
Expand Down
Loading

0 comments on commit 13fed2d

Please sign in to comment.