Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Pure Ruby bit array/bitfield implementation

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 bitarray.gemspec
README.md

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

Build Status

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 http://snippets.dzone.com/posts/show/4234 .. I will review the code and bring the docs up to date in due course.

Installation

gem install bitarray

Examples

To use:

require 'bitarray'

Create a bit array 1000 bits wide:

ba = BitArray.new(1000)

Setting and reading bits:

ba[100] = 1
ba[100]
#=> 1

ba[100] = 0
ba[100]
#=> 0

More:

ba = BitArray.new(20)
[1,3,5,9,11,13,15].each { |i| ba[i] = 1 }
ba.to_s
#=> "01010100010101010000"
ba.total_set
#=> 7

History

  • 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)

License

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

Something went wrong with that request. Please try again.