calabrothers/php-ds-bitmask

1.0.0 2020-01-19 22:44 UTC

This package is auto-updated.

Last update: 2025-03-20 11:09:33 UTC


README

PHP Bitmask Support

Build Status Coverage Status Total Downloads License -->

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:

Operator Notation

AND(a,b)

AND

OR(a,b)

OR

NOT(a)

NOT

Then the basic Bitmask object will provide the following operators:

PHP Command Bitwise Operation
$x->setValue($v)

SetValue

$x->setBit($b)

SetBit

$x->clearBit($b)

ClearBit

$x->checkBit($b)

CheckBit

$x->setMask($m)

SetMask

$x->clearMask($m)

ClearMask

$x->checkMask($m)

CheckMask

$x->not()

NotFcn

$x->or($y)

OrFcn

$x->and($y)

AndFcn

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

Foo

License

The MIT License (MIT). Please see LICENSE for more information.