bitset

Compact bitflags and bit-array manipulation with popcount and bitwise ops.

v1.0.0Data structuresbundledimport "bitset";
Overview Installation Documentation Examples

Overview

Compact BitSet library for HolyPHP providing bitflag and bit-array manipulation, popcount, bitwise set operations, binary string serialization, and clear out-of-bounds error reporting.

bundled — works offline bits flags popcount

Quick start

import "bitset";

$a = new BitSet(8);
$a->set(1);
$a->set(3);
$a->set(5);

$b = bitset_from_indices(8, [3, 5, 7]);

$andSet = $a->bitAnd($b);
$orSet = $a->bitOr($b);
$xorSet = $a->bitXor($b);

println($a->toBinaryString());      // 01010100
println($andSet->toBinaryString()); // 00010100
println($andSet->count());          // 2
println(json_encode($orSet->toIndices())); // [1,3,5,7]

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install bitset

This adds the library to hphp.yaml, installs it into .hphp/packages/bitset/ and pins the exact version in hphp.lock:

require:
  bitset: "^1.0"

Use it

import "bitset";

bitset is bundled with the compiler, so import "bitset"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install bitset          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install bitset
Packagebitset
Version1.0.0 (follows the compiler)
Entry filebitset.hphp
Sourcepackages/lib/bitset ↗

Documentation

API Reference

Class BitSet

Bits are indexed from 0 to size - 1 and stored compactly in 30-bit integer words.

Helper Functions

Error Handling

All validation errors throw an Exception prefixed with bitset::

Examples

01Quick start

Bundled with the compiler — import "bitset"; works offline in any file, or add it to a project with hphp install bitset.

import "bitset";

$a = new BitSet(8);
$a->set(1);
$a->set(3);
$a->set(5);

$b = bitset_from_indices(8, [3, 5, 7]);

$andSet = $a->bitAnd($b);
$orSet = $a->bitOr($b);
$xorSet = $a->bitXor($b);

println($a->toBinaryString());      // 01010100
println($andSet->toBinaryString()); // 00010100
println($andSet->count());          // 2
println(json_encode($orSet->toIndices())); // [1,3,5,7]
Try it
hphp install bitset
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_bitset.hphp.