https://github.com/yaroslavche/bitmask
PHP library for working with bitmask
https://github.com/yaroslavche/bitmask
bitmask bitwise php php8
Last synced: 7 months ago
JSON representation
PHP library for working with bitmask
- Host: GitHub
- URL: https://github.com/yaroslavche/bitmask
- Owner: yaroslavche
- License: mit
- Created: 2018-01-31T15:10:15.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-09-01T07:53:03.000Z (about 1 year ago)
- Last Synced: 2025-02-27T21:16:32.341Z (8 months ago)
- Topics: bitmask, bitwise, php, php8
- Language: PHP
- Homepage:
- Size: 191 KB
- Stars: 32
- Watchers: 3
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/yaroslavche/BitMask/actions/workflows/php.yml)
[](https://codecov.io/gh/yaroslavche/bitmask)
[](https://dashboard.stryker-mutator.io/reports/github.com/yaroslavche/BitMask/main)
[](https://packagist.org/packages/yaroslavche/bitmask)
# BitMaskPHP library for working with bitmask values
## Getting Started
Usually enough for checking bits:
```php
define('READ', 1 << 0);
define('WRITE', 1 << 1);
define('EXECUTE', 1 << 2);$mask = READ | WRITE | EXECUTE;
echo sprintf('mask: %d', $mask); // mask: 7
if ($mask & READ) {} // if $mask has a single bit READ
$mask &= ~EXECUTE; // remove a single bit from the $mask
$mask |= EXECUTE; // set a single bit to the $mask
```But you can try other way with this package:
```php
use BitMask\BitMask;// two arguments: integer mask (default: 0) and most significant bit for boundaries (default: null)
$bitmask = new BitMask(READ | WRITE | EXECUTE);
echo sprintf('mask: %d', $bitmask->get()); // mask: 7
if ($bitmask->has(READ)) {}
$bitmask->remove(EXECUTE);
$bitmask->set(EXECUTE);
```Exists [EnumBitMask](/src/EnumBitMask.php), which allows the same using PHP enum:
```php
use BitMask\EnumBitMask;enum Permissions
{
case READ;
case WRITE;
case EXECUTE;
}// two arguments: required enum class-string and integer mask (default: 0)
$bitmask = new EnumBitMask(Permissions::class, 0b111);
echo sprintf('mask: %d', $bitmask->get()); // mask: 7
if ($bitmask->has(Permissions::READ)) {}
$bitmask->remove(Permissions::EXECUTE);
$bitmask->set(Permissions::EXECUTE);$bitmask->set(Unknown::Case); // throws an exception, only Permissions cases available
````EnumBitMask` have a factory methods:
```php
# Create a bit mask using one or multiple enum cases
$bitmask = EnumBitMask::create(Permissions::class, Permissions::EXECUTE);# Create a bit mask using all enum cases
$bitmask = EnumBitMask::all(Permissions::class);# Create a bit mask with no flags on (equivalent to create with no additional flags)
$bitmask = EnumBitMask::none(Permissions::class);# Create a bit mask without specific flags
$bitmask = EnumBitMask::without(Permissions::class, Permissions::EXECUTE);
```Exists [Bits](/src/Util/Bits.php) helper with static methods:
```php
use BitMask\Util\Bits;$mask = 7; // 1 << 0 | 1 << 1 | 1 << 2
$integerMostSignificantBit = Bits::getMostSignificantBit($mask); // int 2
$arraySetBitsIndexes = Bits::getSetBitsIndexes($mask); // array:3 [0, 1, 2]
$arraySetBits = Bits::getSetBits($mask); // array:3 [1, 2, 4]
$string = Bits::toString($mask); // string "111"
$integerBit = Bits::indexToBit(16); // int 65536
$integerIndex = Bits::bitToIndex(65536); // int 16
$boolIsSingleBit = Bits::isSingleBit(8); // true
```## Installing
Install package via [composer](https://getcomposer.org/)
```bash
composer require yaroslavche/bitmask
```## Contributing
Feel free to fork or contribute =)
#### CI build
```shell
$ composer ci:pack
```#### Tests
##### PHPUnit
```shell
$ composer phpunit
$ ./vendor/bin/phpunit
```
##### Infection
```shell
$ composer infection
$ ./vendor/bin/infection --min-msi=100 --min-covered-msi=100
```
#### Benchmarks
```shell
$ composer phpbench
$ ./vendor/bin/phpbench run benchmarks --report=default
```
#### Static analyzer and code style
##### PHPStan
```shell
$ composer phpstan
$ ./vendor/bin/phpstan analyse src/ -c phpstan.neon --level=9 --no-progress -vvv --memory-limit=1024M
```##### Psalm
```shell
$ composer psalm
$ ./vendor/bin/psalm
```
##### PHP-CS
###### Code style check
```shell
$ composer phpcs-check
$ ./vendor/bin/php-cs-fixer check --diff```
###### Code style fix
```shell
$ composer phpcs-fix
$ ./vendor/bin/php-cs-fixer fix
```## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details