PHPackages                             yaroslavche/bitmask - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yaroslavche/bitmask

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yaroslavche/bitmask
===================

BitMask, EnumBitMask

v3.2.0(5mo ago)3453.7k↑35.6%61MITPHPPHP ^8.1CI passing

Since Jun 6Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/yaroslavche/BitMask)[ Packagist](https://packagist.org/packages/yaroslavche/bitmask)[ RSS](/packages/yaroslavche-bitmask/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (14)Used By (1)

[![PHP](https://camo.githubusercontent.com/2827fc19d15a8e22b0af16b4f3214c2f7e2d3144d3a5ced1deea36c1ebc0f182/687474703a2f2f706f7365722e707567782e6f72672f7961726f736c61766368652f6269746d61736b2f726571756972652f706870)](https://packagist.org/packages/yaroslavche/bitmask)[![Latest Stable Version](https://camo.githubusercontent.com/50430a0e1b8e7d055af66c6a7326bd1566f34fdb60ed07634916c910edf284e3/68747470733a2f2f706f7365722e707567782e6f72672f7961726f736c61766368652f6269746d61736b2f762f737461626c65)](https://packagist.org/packages/yaroslavche/bitmask)[![PHP build](https://github.com/yaroslavche/BitMask/actions/workflows/php.yml/badge.svg)](https://github.com/yaroslavche/BitMask/actions/workflows/php.yml)[![codecov](https://camo.githubusercontent.com/50e7353cba507ad5305ff2f38511afc410b961c3fda1b32cf6db1bad2a65e842/68747470733a2f2f636f6465636f762e696f2f67682f7961726f736c61766368652f6269746d61736b2f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/yaroslavche/bitmask)[![Infection MSI](https://camo.githubusercontent.com/42e077dea0075ed77de0193ac8136f0df82e11dd7910f2af6adf61bd3bfa910f/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d2532467961726f736c61766368652532464269744d61736b2532466d61696e)](https://dashboard.stryker-mutator.io/reports/github.com/yaroslavche/BitMask/main)[![Total Downloads](https://camo.githubusercontent.com/c5005082a9fe113dfc3d26a6451259ae2794608758ee66ba7cc1d8a96d5226b8/68747470733a2f2f706f7365722e707567782e6f72672f7961726f736c61766368652f6269746d61736b2f646f776e6c6f616473)](https://packagist.org/packages/yaroslavche/bitmask)

BitMask
=======

[](#bitmask)

Lightweight PHP library for working with bitmask values with support for PHP enums.

- Zero dependencies
- PHP ≥ 8.1
- Enum-friendly
- Strict validation (single-bit guarantees)

Installation
------------

[](#installation)

```
composer require yaroslavche/bitmask
```

Why BitMask?
------------

[](#why-bitmask)

Classic bitmask operations are simple but quickly become error-prone and hard to read:

```
$mask &= ~EXECUTE;
$mask |= READ | WRITE;
```

BitMask provides a small, explicit API and optional enum-based safety without hiding how bitmasks actually work.

Basic Usage
-----------

[](#basic-usage)

```
use BitMask\BitMask;

define('READ', 1 remove(WRITE);

if ($bitmask->has(READ)) {
// ...
}

echo $bitmask->get(); // 5
```

EnumBitMask (recommended)
-------------------------

[](#enumbitmask-recommended)

Work with bitmasks using PHP enums.

### Unit enums (auto-mapped)

[](#unit-enums-auto-mapped)

```
use BitMask\EnumBitMask;

enum Permissions
{
    case READ;
    case WRITE;
    case EXECUTE;
}

$mask = new EnumBitMask(Permissions::class);

$mask->set(Permissions::READ);
$mask->set(Permissions::EXECUTE);

$mask->has(Permissions::WRITE); // false
$mask->get(); // 5
```

Enum cases are mapped automatically based on their position: `READ` → `1`, `WRITE` → `2`, `EXECUTE` → `4`.

### Int-backed enums

[](#int-backed-enums)

```
enum Flags: int
{
    case User  = 1;   // 0b0001
    case Admin = 8;   // 0b1000
}

$mask = new EnumBitMask(Flags::class, Flags::User | Flags::Admin);
$mask->get(); // 9
```

Each enum value must represent a single bit. Otherwise, an `InvalidEnumException` will be thrown.

Factory Methods
---------------

[](#factory-methods)

```
EnumBitMask::create(Permissions::class, Permissions::READ, Permissions::EXECUTE);
EnumBitMask::all(Permissions::class);
EnumBitMask::none(Permissions::class);
EnumBitMask::without(Permissions::class, Permissions::EXECUTE);
```

Bits Helper
-----------

[](#bits-helper)

Low-level static helpers for working with integer bitmasks.

```
use BitMask\Util\Bits;

Bits::getMostSignificantBit(7); // 2
Bits::getSetBitsIndexes(7);     // [0, 1, 2]
Bits::getSetBits(7);            // [1, 2, 4]
Bits::toString(7);              // "111"
Bits::isSingleBit(8);           // true
```

Contributing
------------

[](#contributing)

Development and contribution guidelines are described in [CONTRIBUTING.md](CONTRIBUTING.md)

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance72

Regular maintenance activity

Popularity41

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 98.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~198 days

Recently: every ~281 days

Total

13

Last Release

156d ago

Major Versions

0.2.0 → v1.0.02021-11-12

v1.1.0 → v2.0.02022-10-30

v2.0.1 → v3.0.02022-11-14

PHP version history (4 changes)0.1.1PHP ^7.2

v1.0.0PHP ^7.4|^8.0

v1.0.1PHP ^7.4|^8.0|^8.1

v2.0.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/17838239?v=4)[Yaroslav](/maintainers/yaroslavche)[@yaroslavche](https://github.com/yaroslavche)

---

Top Contributors

[![yaroslavche](https://avatars.githubusercontent.com/u/17838239?v=4)](https://github.com/yaroslavche "yaroslavche (124 commits)")[![gisostallenberg](https://avatars.githubusercontent.com/u/11526270?v=4)](https://github.com/gisostallenberg "gisostallenberg (2 commits)")

---

Tags

bitmaskbitwisephpphp8phpbinaryphp8bitbitmaskbitwise

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/yaroslavche-bitmask/health.svg)

```
[![Health](https://phpackages.com/badges/yaroslavche-bitmask/health.svg)](https://phpackages.com/packages/yaroslavche-bitmask)
```

###  Alternatives

[marcin-orlowski/lombok-php

Never write boilerplate code for your data class again!

3118.6k1](/packages/marcin-orlowski-lombok-php)[andreas-glaser/php-helpers

A comprehensive collection of PHP utility functions for array manipulation, string operations, date handling, HTML generation, form building, validation, and more. Modern PHP 8.2+ library with full type safety.

1386.5k2](/packages/andreas-glaser-php-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
