All Projects → yaroslavche → BitMask

yaroslavche / BitMask

Licence: MIT license
PHP library for working with bitmask

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to BitMask

Binary-Calculator-JavaScript
📱 A handy Calculator for Binary operations, that works on all Devices 📱 💻 🖥 | ⛓ https://play.google.com/store/apps/details?id=com.binarycalculator.ayidouble.binarycalculator.app ⛓
Stars: ✭ 45 (+125%)
Mutual labels:  bitwise
TeamReference
Team reference for Competitive Programming. Algorithms implementations very used in the ACM-ICPC contests. Latex template to build your own team reference.
Stars: ✭ 29 (+45%)
Mutual labels:  bitmask
ecs
Build your own Game-Engine based on the Entity Component System concept in Golang.
Stars: ✭ 68 (+240%)
Mutual labels:  bitmask
standards-and-practices
Standards and Practices for Bitwise Industries
Stars: ✭ 52 (+160%)
Mutual labels:  bitwise
permer
🔑 A basic abstraction for handling flags using bitwise
Stars: ✭ 41 (+105%)
Mutual labels:  bitwise

PHP build codecov Infection MSI

BitMask

PHP library for working with bitmask values

Getting Started

Usually enough for checking bits:

define('READ', 1 << 0);
define('WRITE', 1 << 1);
define('EXECUTE', 1 << 2);
$mask = READ | WRITE | EXECUTE;
// read: 1 write: 2 execute: 4 mask: 7
echo sprintf('read: %d write: %d execute: %d mask: %d', READ, WRITE, EXECUTE, $mask);
if ($mask & READ) {
    // $mask have a READ
}

But you can try other way with this package:

use BitMask\BitMask;
use BitMask\Util\Bits;

$bitmask = new BitMask();
$bitmask->set(0b111); // 7, 1 << 0 | 1 << 1 | 1 << 2

// get value and check if single bit or mask is set 
$integerMask = $bitmask->get(); // int 7
$boolIsSetBit = $bitmask->isSetBit(4); // bool true
$boolIsSetBit = $bitmask->isSetBitByShiftOffset(2); // true
$boolIsSetMask = $bitmask->isSet(6); // bool true

// get some info about bits
$integerMostSignificantBit = Bits::getMostSignificantBit($bitmask->get()); // int 3
$arraySetBits = Bits::getSetBits($bitmask->get()); // array:3 [1, 2, 4]
$arraySetBitsIndexes = Bits::getSetBitsIndexes($bitmask->get()); // array:3 [0, 1, 2]
$string = Bits::toString($bitmask->get()); // string "111"

// some helpers
$integerBit = Bits::indexToBit(16); // int 65536
$integerIndex = Bits::bitToIndex(65536); // int 16
$boolIsSingleBit = Bits::isSingleBit(8); // true

// change mask 
$bitmask->unsetBit(4);
$bitmask->unsetBitByShiftOffset(2);
$bitmask->setBit(8);

Bits::getSetBits($bitmask->get()); // array:3 [1, 2, 8]

Some examples can be found in BitMaskInterface and in tests

Exists IndexedBitMask and AssociativeBitMask helper classes:

use BitMask\IndexedBitMask;
use BitMask\AssociativeBitMask;

// Indexed are extended BitMask with one extra method: getByIndex
// For instance, mask 0b110 would have following "index:value": 0:false, 1:true, 2:true
// Indexes are RTL, starts from 0. Equals to mask left shift offset.
$indexed = new IndexedBitMask(1 << 1 | 1 << 2); // 0b110
$indexed->getByIndex(2); // true
$indexed->getByIndex(0); // false

// Associative are extended Indexed. In addition to the mask you must also specify the number of bits and the array of key strings.
// Each key will have a bitmask property with the same name and a method named 'is{Key}'.
$bitmask = new AssociativeBitMask(5, 3, ['readable', 'writable', 'executable']); // 
$bitmask->getByKey('readable'); // bool(true)
/** __call */
$boolReadable = $bitmask->isReadable(); // bool(true)
$boolWritable = $bitmask->isWritable(); // bool(true)
$boolExecutable = $bitmask->isExecutable(); // bool(true)
$result = $bitmask->isUnknownKey(); // BitMask\Exception\UnknownKeyException
/** __get */
$boolReadable = $bitmask->readable; // bool true
$boolWritable = $bitmask->writable; // bool false
$boolExecutable = $bitmask->executable; // bool true
$result = $bitmask->unknownKey; // BitMask\Exception\UnknownKeyException
/** __set */
$bitmask->readable = false;
$bitmask->writable = true;
$bitmask->executable = false;
$bitmask->unknownKey = true; // BitMask\Exception\UnknownKeyException

Installing

Install package via composer

composer require yaroslavche/bitmask

Contributing

Feel free to fork or contribute =)

Tests

PHPUnit
$ composer phpunit
$ ./vendor/bin/phpunit
Infection
$ composer infection
$ ./vendor/bin/infection --min-msi=100 --min-covered-msi=100

Benchmarks

$ composer phpbench
$ ./vendor/bin/phpbench run benchmarks --report=default

Static analyzer and code style

PHPStan
$ composer phpstan
$ ./vendor/bin/phpstan analyse src/ -c phpstan.neon --level=8 --no-progress -vvv --memory-limit=1024M
PHP-CS
Code style check
$ composer phpcs
$ ./vendor/bin/phpcs
Code style fix
$ composer phpcbf
$ ./vendor/bin/phpcbf

License

This project is licensed under the MIT License - see the LICENSE file for details

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].