A bit array class for Ruby, implemented as a C extension. Includes methods for setting and clearing individual bits, and all bits at once. Also has the standard array access methods, [] and []=, and it mixes in Enumerable.

Example usage:

require 'bitarray'
bm = BitArray.new(16)
bm[5] = 1
bm.toggle_all_bits
puts bm

The test/ directory has a unit test file. It also has a benchmark utility for comparison with Peter Cooper’s pure Ruby BitField class.

This library has been compiled and tested on:

ruby 1.9.1p129 (2009-05-12 revision 23412) [i686-linux]
ruby 1.8.7 (2008-08-11 patchlevel 72) [alpha-netbsd]

If you have success using it on any other version/platform combinations, I’d like to know. Or if you have problems, I’d like to know that too. Report bugs at github.com/ingramj/bitarray/issues

BitArray is available as a gem. Install it with

sudo gem install ingramj-bitarray -s gems.github.com

Alternately, you can clone the repository and build things manually.

See the TODO file for current limitations and plans.

See the LICENSE file for copyright and license information.

Some inspiration was taken from Peter Cooper’s BitField class, and various Open Source bitarray implementations found online, but this implementation contains only original code, except for test/bitfield.rb, which is Peter Cooper’s BitField class, and test/test.rb, which was modified from the BitField test code.

BitField: snippets.dzone.com/posts/show/4234