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
Documentation
API Reference
Class BitSet
Bits are indexed from 0 to size - 1 and stored compactly in 30-bit integer words.
new BitSet(int $size) Creates a zero-initialized BitSet with capacity $size. Throws if $size < 0.
size(): int Returns the total number of bits in the BitSet.
set(int $index): void Sets the bit at $index to 1.
clear(int $index): void Clears the bit at $index to 0.
toggle(int $index): void Flips the bit at $index (0 to 1, or 1 to 0).
get(int $index): bool Returns true if the bit at $index is 1, false otherwise.
setAll(): void Sets all bits in [0, size - 1] to 1.
clearAll(): void Clears all bits to 0.
count(): int Returns the population count (number of bits set to 1).
isEmpty(): bool Returns true if no bits are set.
cloneSet(): BitSet Returns an independent copy of the BitSet.
equals(BitSet $other): bool Returns true if both BitSet instances have the same size and identical bit values.
bitAnd(BitSet $other): BitSet Returns a new BitSet containing the bitwise AND of $this and $other.
bitOr(BitSet $other): BitSet Returns a new BitSet containing the bitwise OR of $this and $other.
bitXor(BitSet $other): BitSet Returns a new BitSet containing the bitwise XOR of $this and $other.
bitNot(): BitSet Returns a new BitSet with all bits inverted.
toBinaryString(): string Returns a string of '0' and '1' characters from index 0 to size - 1.
toIndices(): array Returns a sorted array of integer indices where the bit is 1.
Helper Functions
bitset_create(int $size): BitSet Convenience constructor for new BitSet($size).
bitset_from_binary_string(string $bits): BitSet Creates a BitSet from a string of '0' and '1' characters (index 0 is the leftmost character).
bitset_from_indices(int $size, array $indices): BitSet Creates a BitSet of size $size with each index in $indices set to 1.
Error Handling
All validation errors throw an Exception prefixed with bitset::
- Negative size:
bitset: size must be non-negative, got -1
- Out-of-bounds index on
set, clear, toggle, or get: bitset: index 8 out of bounds for size 8
- Size mismatch on
bitAnd, bitOr, or bitXor: bitset: cannot perform bitAnd on BitSets of different sizes (8 vs 16)
- Invalid character in
bitset_from_binary_string: bitset: invalid character '2' at index 3 in binary string (expected '0' or '1')
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 ithphp install bitset
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_bitset.hphp.