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

bm = BitArray.new("10010110")
bm.toggle_bit 3
bm.clear_bit 0

The test/ directory has a unit test file, and benchmarking utility.

The examples/ directory has bloom filter dictionary-lookup demonstration.

This library has been compiled and tested on:

ruby 1.9.1p129 (2009-05-12 revision 23412) [i686-linux]  (Ubuntu from src)
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]       (Ubuntu apt-get)
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 some parts for test.rb, which were modified from BitField’s unit test file.

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