|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Util; |
|
4
|
|
|
|
|
5
|
|
|
use Jabe\Engine\Impl\ProcessEngineLogger; |
|
6
|
|
|
|
|
7
|
|
|
class BitMaskUtil |
|
8
|
|
|
{ |
|
9
|
|
|
//private static final EngineUtilLogger LOG = ProcessEngineLogger.UTIL_LOGGER; |
|
10
|
|
|
|
|
11
|
|
|
// First 8 masks as constant to prevent having to math.pow() every time a bit needs flippin'. |
|
12
|
|
|
private const FLAG_BIT_1 = 1; // 000...00000001 |
|
13
|
|
|
private const FLAG_BIT_2 = 2; // 000...00000010 |
|
14
|
|
|
private const FLAG_BIT_3 = 4; // 000...00000100 |
|
15
|
|
|
private const FLAG_BIT_4 = 8; // 000...00001000 |
|
16
|
|
|
private const FLAG_BIT_5 = 16; // 000...00010000 |
|
17
|
|
|
private const FLAG_BIT_6 = 32; // 000...00100000 |
|
18
|
|
|
private const FLAG_BIT_7 = 64; // 000...01000000 |
|
19
|
|
|
private const FLAG_BIT_8 = 128; // 000...10000000 |
|
20
|
|
|
|
|
21
|
|
|
private const MASKS = [self::FLAG_BIT_1, self::FLAG_BIT_2, self::FLAG_BIT_3, self::FLAG_BIT_4, self::FLAG_BIT_5, self::FLAG_BIT_6, self::FLAG_BIT_7, self::FLAG_BIT_8]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Set bit to '1' in the given int. |
|
25
|
|
|
* @param current integer value |
|
26
|
|
|
* @param bitNumber number of the bit to set to '1' (right first bit starting at 1). |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function setBitOn(int $value, int $bitNumber): int |
|
29
|
|
|
{ |
|
30
|
|
|
self::ensureBitRange($bitNumber); |
|
31
|
|
|
// To turn on, OR with the correct mask |
|
32
|
|
|
return $value | self::MASKS[$bitNumber - 1]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Set bit to '0' in the given int. |
|
37
|
|
|
* @param current integer value |
|
38
|
|
|
* @param bitNumber number of the bit to set to '0' (right first bit starting at 1). |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function setBitOff(int $value, int $bitNumber): int |
|
41
|
|
|
{ |
|
42
|
|
|
self::ensureBitRange($bitNumber); |
|
43
|
|
|
// To turn on, OR with the correct mask |
|
44
|
|
|
return $value & ~self::MASKS[$bitNumber - 1]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Check if the bit is set to '1' |
|
49
|
|
|
* @param value integer to check bit |
|
50
|
|
|
* @param number of bit to check (right first bit starting at 1) |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function isBitOn(int $value, int $bitNumber): bool |
|
53
|
|
|
{ |
|
54
|
|
|
self::ensureBitRange($bitNumber); |
|
55
|
|
|
return (($value & self::MASKS[$bitNumber - 1]) == self::MASKS[$bitNumber - 1]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set bit to '0' or '1' in the given int. |
|
60
|
|
|
* @param current integer value |
|
61
|
|
|
* @param bitNumber number of the bit to set to '0' or '1' (right first bit starting at 1). |
|
62
|
|
|
* @param bitValue if true, bit set to '1'. If false, '0'. |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function setBit(int $value, int $bitNumber, bool $bitValue): int |
|
65
|
|
|
{ |
|
66
|
|
|
if ($bitValue) { |
|
67
|
|
|
return self::setBitOn($value, $bitNumber); |
|
68
|
|
|
} else { |
|
69
|
|
|
return self::setBitOff($value, $bitNumber); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function getMaskForBit(int $bitNumber): int |
|
74
|
|
|
{ |
|
75
|
|
|
return self::MASKS[$bitNumber - 1]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private static function ensureBitRange(int $bitNumber): void |
|
79
|
|
|
{ |
|
80
|
|
|
if ($bitNumber <= 0 && $bitNumber > 8) { |
|
81
|
|
|
//throw LOG.invalidBitNumber(bitNumber); |
|
82
|
|
|
throw new \Exception("invalidBitNumber"); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|