aliance / bitmask
Simple bitmask implementation
Requires
- php-64bit: >=8.3
Requires (Dev)
- phpunit/phpunit: ^12
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
About
Bitmask is a simple PHP implementation of bitwise operations for creating masks. Can be used for some flags' implementation. Supports only 64 bits (from 0 to 63) on x64 platforms.
Installation
Install the latest version with composer:
composer require aliance/bitmask
If you checkout this library for testing purposes, install its dependencies:
docker run --rm -it --volume $PWD:/app -u $(id -u):$(id -g) composer:2 i
Usage
Pass a bit position (0 through 63) to setBit(), unsetBit() and issetBit().
Pass an integer mask to addMask() and deleteMask(). For example, starting from an empty mask:
use Aliance\Bitmask\Bitmask; (new Bitmask())->setBit(3)->getMask(); // 8 (bit 3) (new Bitmask())->addMask(3)->getMask(); // 3 (bits 0 and 1)
The permission example uses an integer-backed enum to name bit positions.
It loads a user with Create permission, grants Read, and saves the updated value with getMask().
Loading that value restores both permissions.
PHP uses signed integers. Setting bit 63 makes the mask negative.
Use a signed 64-bit column such as BIGINT to store the full range and pass the signed value back to the constructor when reading it.
docker run --rm -v "$PWD":/usr/src/bitmask -w /usr/src/bitmask php:8.3-cli php example/example.php
Stored mask: 3
Create: yes
Read: yes
Update: no
Delete: no
Tests
For completely tests running just call composer exec phpunit or use
docker run -it --rm -v "$PWD":/usr/src/bitmask -w /usr/src/bitmask php:8.3-cli php ./vendor/bin/phpunit
License
This software is distributed under MIT license.