GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - peterc/bitarray: Pure Ruby bit array/bitfield implementation

BranchesTags
Open more actions menu

Latest commit

 

History

57 Commits
57 Commits

Folders and files

NameName
Last commit message
Last commit date
lib
lib
 
 
test
test
 
 
.gitignore
.gitignore
 
 
Gemfile
Gemfile
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
Rakefile
Rakefile
 
 
bitarray.gemspec
bitarray.gemspec
 
 
memory-test.rb
memory-test.rb
 
 

Repository files navigation

BitArray: Pure Ruby bit-array/bitfield library

A simple, pure-Ruby 'bit field' object.

Originally built to help power a bloom filter, although there are other higher level libraries for that task now (https://github.com/igrigorik/bloomfilter-rb is a popular one.)

BitArray has changed little over the years, but has been maintained to work within a typical, modern Ruby environment and works with Ruby 2.1+ through Ruby 3.x.

Installation

bundle add 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

Initializing BitArray with a custom field value:

ba = BitArray.new(16, ["0000111111110000"].pack('B*'))
ba.to_s # "1111000000001111"

BitArray by default stores the bits in reverse order for each byte. If for example, you are initializing BitArray with Redis raw value manipulated with setbit / getbit operations, you will need to tell BitArray to not reverse the bits in each byte using the reverse_byte: false option:

ba = BitArray.new(16, ["0000111111110000"].pack('B*'), reverse_byte: false)
ba.to_s # "0000111111110000"

History

  • 1.3.2 in 2026 (minor cleanup, faster total_set)
  • 1.3.1 in 2024 (no changes other than adding license to gemspec)
  • 1.3 in 2022 (cleanups and a minor perf tweak)
  • 1.2 in 2018 (Added option to skip reverse the bits for each byte by @dalibor)
  • 1.1 in 2018 (fixed a significant bug)
  • 1.0 in 2017 (updated for modern Ruby, more efficient storage, and 10th birthday)
  • 0.0.1 in 2012 (original v5 released on GitHub)
  • 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)

Thanks

Thanks to Michael Slade for encouraging me to update this library on its 10th birthday and for suggesting finally using String's getbyte and setbyte methods now that we're all on 1.9+ compatible implementations.

Further thanks to @tdeo, @JoshuaSP, @dalibor, @yegct and @m1lt0n for pull requests.

License

MIT licensed. Copyright 2007-2026 Peter Cooper.

About

Pure Ruby bit array/bitfield implementation

Resources

Readme
MIT license
Activity

Stars

107 stars

Watchers

2 watching

Forks

14 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.