PHP Classes

bitvec: Manipulate arrays of bits with SplFixedArray class

Recommend this page to a friend!
  Info   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 178 All time: 8,723 This week: 274Up
Version License PHP version Categories
bitvec 1.0Public Domain5.0PHP 5, Data types


This package can manipulate arrays of bits with SplFixedArray class.

It provides an array interface to store a given fixed number of bits.

The array of bits can be traversed with an iterator interface that allows accessing the bits with regular functions for accessing arrays.

Innovation Award
PHP Programming Innovation award nominee
January 2013
Number 5

Prize: One downloadable copy of Komodo IDE
The regular arrays in PHP are implemented internally as hash tables.

This means that the access to any array element access requires the computation of a hash value and the eventual traversal of a linked list of array elements.

For arrays with entries indexed by their numeric position, it is faster to use the SplFixedArray class.

This class uses the SplFixedArray class to provide a faster interface to access arrays of bits.

Manuel Lemos
Picture of CPK Smithies
  Performance   Level  
Name: CPK Smithies <contact>
Classes: 7 packages by
Country: United Kingdom
Age: 69
All time rank: 152069 in United Kingdom
Week rank: 367 Up12 in United Kingdom Up
Innovation award
Innovation award
Nominee: 4x


PHP bit vector (array of 0/1 values) designed and written by CPKS 1. Introduction: The bitvec class is a specialized array implementation for storing 0/1 values only. Because of this limitation, it is possible to store multiple values in a single integer. Use of SplFixedArray provides a memory-efficient vehicle to deliver a relatively high capacity bit array. A separate iterator class, bitvecIterator, has been provided to provide full array iterator access to bitvec. For many applications this may be unnecessary and the bitvec class alone will suffice. Bit vectors are useful for certain numeric algorithms, and in particular for the generation of primes using sieve techniques, where it can be advantageous to hold a very large bit vector in memory. 2. Acknowledgments: One application for bit vectors is the generation of primes. I signal my indebtedness to for the prime.php file on this site, which provided a launchpad for my rewritten version, used as a demo and test file in this package. 3. Licensing: On licensing, please see the LICENCE file issued with this package. 4. Files in this package: 4.1 bitvec.php: bitvec and bitvecIterator classes 4.2 LICENCE: licensing details 4.3 phpunit.xml.dist: test configuration 4.4 phpdoc.dist.xml: Assists the phpdoc documentation-generating package in producing documentation for this package. 4.5 README: this file 4.6 tests/bvt1.php: Unit tests for bitvec class 4.7 tests/prime.php: Prime generation using bitvec according to several different algorithms, provided as illustration of class use and as part of unit testing. 5. Documentation I have elected to tailor this package to the PEAR phpdoc documentation system. This produces good interactive HTML browsing capabilities together with an SVG class diagram showing package and namespace relationships as well as the object inheritance/implementation tree. See for installation instructions. To generate the documentation, create a directory called "doc" in the project base directory, and then run the phpdoc command in that base directory. 6. Historical and design-related remarks on this package The bitvec class is loosely based on my C++ class of the same name from 1991. Although the PHP bitvec uses SplFixedArray, note that it does not extend the SPL class. This is because some of the SplFixedArray functions really do not correspond at all to bitvec functionality: there is no "is-a" relationship here: a bitvec is not an SplFixedArray and certain operations mean something different. In particular, no use is made of the SplFixedArray iterator implementation, which has in any case been removed to bitvecIterator. No use has been made of namespaces. Users of PHP >= 5.3 are recommended to bring bitvec inside a namespace, e.g. storage. The code is ready for this to be done. Array bound violations are always caught by the SplFixedArray. Note that this throws RuntimeException and not OutOfBoundsException. CPKS

  Files folder image Files  
File Role Description
Files folder imagetests (2 files)
Plain text file bitvec.php Class Bitvec class source
Accessible without login Plain text file LICENCE Doc. Licence terms
Accessible without login Plain text file phpdoc.dist.xml Data Configuration for generating the HTML documentation
Accessible without login Plain text file phpunit.xml.dist Data Unit test configuration
Accessible without login Plain text file README Doc. About the package

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file bvt1.php Test Unit tests on bitvec, demo prime generation
  Accessible without login Plain text file prime.php Example Demo using bitvec to generate primes using sieve techniques

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:8,723
This week:274Up