0.0
The project is in a healthy, maintained state
Extends String with methods for reading, iterating, and manipulating individual bits in packed binary data.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

string_bits gem

Ruby's String is already a byte sequence. This gem extends it one level lower: a bit sequence --- making packed binary buffers first-class values without any new class.

The methods are designed for real workloads: Apache Arrow validity bitmaps, bitmap font glyph data, and any protocol buffer where bits carry meaning. Many methods read or modify the existing string bytes directly; methods such as bit_slice, bitwise_not, bitwise_and, bitwise_or, bitwise_xor, bits, bit_offsets, bit_runs, and Array#mask allocate a derived result object. Because the API relies solely on String, the same methods would benefit memory-constrained implementations such as mruby and PicoRuby once adopted into CRuby.

A single keyword, lsb_first:, controls bit ordering across the API: it selects intra-byte numbering wherever positions are exchanged with the caller, and intra-byte scan direction wherever the API walks the sequence. Across-byte order is always byte[0] first, and result Strings from bit_slice are always packed LSB-first regardless, so data.each_bit_offset(true, lsb_first: false).all? { |n| data.bit_at(n, lsb_first: false) } stays true.

Usage

gem install string_bits
require "string_bits"
puts "\xAA\xAA\xAA\xAA".bit_at(10)
#=> false

puts "\xAA".each_bit(lsb_first: false).to_a.inspect
#=> [true, false, true, false, true, false, true, false]