calabrothers / php-ds-bitmask
PHP Bitmask Support
1.0.0
2020-01-19 22:44 UTC
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-10-20 10:21:10 UTC
README
PHP Bitmask Support
A class to support bitmask operations.
Install
composer require calabrothers/php-ds-bitmask
Test
composer install
composer test
HowTo
Building an object is done via constructor or factory function:
$X = new Bitmask(3); // 000...0011
$Y = Bitmask::create(5); // 000...0101
Available functions:
Let us consider the classic three logical operators:
Then the basic Bitmask object will provide the following operators:
Example
$X = new Bitmask(3);
echo $X; // Bitmask(64|62|2) [ 0000 0011 ]
$Y = Bitmask::create(5);
echo $Y; // Bitmask(64|61|3) [ 0000 0101 ]
// Return new object
echo $X->and($Y); // Bitmask(64|61|3) [ 0000 0101 ]
echo $X->and($Y); // Bitmask(64|61|3) [ 0000 0111 ]
// Alter the object
echo $X->clearBit(0); // Bitmask(64|62|2) [ 0000 0010 ]
echo $X->setBit(4); // Bitmask(64|59|5) [ 0001 0010 ]
// Check bit (convention starts from 0)
echo $X->checkBit(2) ? "true":"false"; // false
echo $X->checkBit(4) ? "true":"false"; // true
Credits
Support quality code
License
The MIT License (MIT). Please see LICENSE for more information.