PHPackages                             pwm/bitset - 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. pwm/bitset

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

pwm/bitset
==========

A simple Bit Set implementation

1.1.0(8y ago)54MITPHPPHP &gt;=7.1.0

Since Jul 9Pushed 8y ago1 watchersCompare

[ Source](https://github.com/pwm/bitset)[ Packagist](https://packagist.org/packages/pwm/bitset)[ Docs](https://github.com/pwm/bitset)[ RSS](/packages/pwm-bitset/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

BitSet
======

[](#bitset)

[![Build Status](https://camo.githubusercontent.com/6c51bb814fb9defe7c572456f4413e5a3bae514e8ddae373613cbf84a0de0eff/68747470733a2f2f7472617669732d63692e6f72672f70776d2f6269747365742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/pwm/bitset)[![codecov](https://camo.githubusercontent.com/7926e9852d4a196bfefd835478be509a794a4265c0ffab9c670066d42dc91add/68747470733a2f2f636f6465636f762e696f2f67682f70776d2f6269747365742f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/pwm/bitset)[![Maintainability](https://camo.githubusercontent.com/50570ca0059af2ff1a53a35a503036d108d5bc7c4fcc03a84cc6be18f3c278b6/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f38613964343730326162303533383337376466632f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/pwm/bitset/maintainability)[![Test Coverage](https://camo.githubusercontent.com/34ae42cd38db27dcc0e05156b4efcefeaccabe245dc8cc763fc8ecd059e02da5/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f38613964343730326162303533383337376466632f746573745f636f766572616765)](https://codeclimate.com/github/pwm/bitset/test_coverage)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

A simple bit set implementation for compact storage of sets of values. The library itself is stateless providing only pure mapping functions.

Table of Contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [How it works](#how-it-works)
- [Tests](#tests)
- [Changelog](#changelog)
- [Licence](#licence)

Requirements
------------

[](#requirements)

PHP 7.1+

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

[](#installation)

```
$ composer require pwm/bitset

```

Usage
-----

[](#usage)

In this example we are going to use a bit set to put users in groups.

Let's create a `Group` class. It has a list of groups, named G1 to G5 for this example, and a map between groups and binary values of powers of 2 provided by BitSet. Finally it has 2 functions that map between group names and bit values.

```
class Group
{
    const G1 = 'Group 1';
    const G2 = 'Group 2';
    const G3 = 'Group 3';
    const G4 = 'Group 4';
    const G5 = 'Group 5';

    private const MAP = [
        self::G1 => BitSet::B1,
        self::G2 => BitSet::B2,
        self::G3 => BitSet::B3,
        self::G4 => BitSet::B4,
        self::G5 => BitSet::B5,
    ];

    public static function fromGroups(array $groups): array
    {
        return array_values(array_intersect_key(self::MAP, array_flip($groups)));
    }

    public static function toGroups(array $bitValues): array
    {
        return array_values(array_intersect_key(array_flip(self::MAP), array_flip($bitValues)));
    }
}
```

Let's also create a `User` class. A user can belong to groups. The `$groups` property starts from 0 that represents the empty set. It has 5 methods to manipulate its groups, corresponding to BitSet's 5 functions: `get()`, `set()`, `add()`, `remove()` and `has()`.

```
class User
{
    private $groups = BitSet::EMPTY;

    public function getGroups(): array
    {
        return Group::toGroups(BitSet::get($this->groups));
    }

    public function setGroups(array $groups): void
    {
        $this->groups = BitSet::set(Group::fromGroups($groups));
    }

    public function addGroups(array $groups): void
    {
        $this->groups = BitSet::add($this->groups, Group::fromGroups($groups));
    }

    public function removeGroups(array $groups): void
    {
        $this->groups = BitSet::remove($this->groups, Group::fromGroups($groups));
    }

    public function hasGroup(string $group): bool
    {
        $a = Group::fromGroups([$group]);
        return isset($a[0])
            ? BitSet::has($this->groups, $a[0])
            : false;
    }
}
```

Now we can use the above the following way:

```
$user = new User();
$user->setGroups([Group::G1, Group::G2]);

assert($user->getGroups() === [Group::G1, Group::G2]);
assert($user->hasGroup(Group::G1) === true);

$user->addGroups([Group::G4, Group::G5]);
$user->removeGroups([Group::G1, Group::G4]);

assert($user->getGroups() === [Group::G2, Group::G5]);
assert($user->hasGroup(Group::G1) === false);
```

How it works
------------

[](#how-it-works)

As an example let's take the numbers 1, 4 and 8. Their sum is 13. Pretty uninteresting so far. But let's look at their binary representations: 0b1, 0b100 and 0b1000 respectively. Their sum 13 is 0b1101. See how the base-2 representations of 1, 4 and 8 "fill unique slots" in 13's, leaving only one slot unset? This is because 1, 4 and 8 are all powers of 2 so their binary representations starts with a 1 followed by zero or more 0s. 0b1 = 2^0 = 1, 0b100 = 2^2 = 4, 0b1000 = 2^3 = 8. Their sum: 2^0 + 2^2 + 2^3 = 0b1 + 0b100 + 0b1000 = 0b1101 = 13. Now, say in a game, we could call 1 "north", 2 "south", 4 "east" and 8 "west" and then 13 could mean that our character can move all directions from its current position except south as 2 (0b10) is not in 13 (0b1101).

The idea behind a bit set is using a base-2 representation of a number to singal the presence or absence of values by having its bits set to 1 or 0. The values themselves are also numbers with the restriction that they must be powers of 2 starting from 2^0 and in general, depending on the underlying system, going up to 2^32 or 2^64. For example on a 64 bit system one can store up to 64 unique values in a bit set.

The `BitSet` library itself is a collection of 5 pure functions that map between values and their bit set:

`set(array $values): int`

Maps a list of unique positive integers to a single integer, the bit set, that is their sum. All integers in the input list must be powers of 2.

`get(int $bitSet): array`

Maps an integer, the bit set, to a list of unique positive integers. All integers in the output list must be powers of 2.

`add(int $bitSet, array $values): int`

Adds a list of unique positive integers to a bit set. All integers in the list must be powers of 2.

`remove(int $bitSet, array $values): int`

Subtracts a list of unique positive integers from a bit set. All integers in the list must be powers of 2.

`has(int $bitSet, int $value): bool`

Predicate that tells whether an element is in a bit set or not.

Tests
-----

[](#tests)

```
$ vendor/bin/phpunit
$ composer phpcs
$ composer phpstan

```

Changelog
---------

[](#changelog)

[Click here](changelog.md)

Licence
-------

[](#licence)

[MIT](LICENSE)

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~12 days

Total

2

Last Release

3264d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/195513?v=4)[Zsolt Szende](/maintainers/pwm)[@pwm](https://github.com/pwm)

---

Top Contributors

[![pwm](https://avatars.githubusercontent.com/u/195513?v=4)](https://github.com/pwm "pwm (12 commits)")

---

Tags

bitarraybitmapbitsetbitmapbitsetbitarray

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pwm-bitset/health.svg)

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

###  Alternatives

[sokil/php-bitmap

Bitmap representation with bitwise operations

124.1k2](/packages/sokil-php-bitmap)[codefog/contao-events_subscriptions

events\_subscriptions extension for Contao Open Source CMS

102.7k](/packages/codefog-contao-events-subscriptions)

PHPackages © 2026

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