Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
59 lines (42 sloc) 1.22 KB

BitArray: A simple bit array/bit field library in pure Ruby

Basic, pure Ruby bit field. Pretty fast (for what it is) and memory efficient. Works well for Bloom filters (the reason I wrote it).

Written in 2007 and not updated since then, just bringing it on to GitHub by user request. It used to be called BitField and was hosted at .. I will review the code and bring the docs up to date in due course.


gem install bitarray


To use:

require 'bitarray'

Create a bit array 1000 bits wide:

ba =

Setting and reading bits:

ba[100] = 1
#=> 1

ba[100] = 0
#=> 0


ba =
[1,3,5,9,11,13,15].each { |i| ba[i] = 1 }
#=> "01010100010101010000"
#=> 7


  • v5 (added support for flags being on by default, instead of off)
  • v4 (fixed bug where setting 0 bits to 0 caused a set to 1)
  • v3 (supports dynamic bitwidths for array elements.. now doing 32 bit widths default)
  • v2 (now uses 1 << y, rather than 2 ** y .. it's 21.8 times faster!)
  • v1 (first release)


MIT licensed. Copyright 2007-2013 Peter Cooper, yada yada.

Jump to Line
Something went wrong with that request. Please try again.