Skip to content

struct BitArray
inherits Struct

BitArray is an array data structure that compactly stores bits.

Bits externally represented as Bools are stored internally as UInt32s. The total number of bits stored is set at creation and is immutable.

Example

require "bit_array"

ba = BitArray.new(12) # => "BitArray[000000000000]"
ba[2]                 # => false
0.upto(5) { |i| ba[i * 2] = true }
ba    # => "BitArray[101010101010]"
ba[2] # => true

Included modules

Indexable

Class methods

.new(size, initial : Bool = false)

Creates a new BitArray of size bits.

initial optionally sets the starting value, true or false, for all bits in the array.

View source

Methods

#==(other : BitArray)

View source

#[](start : Int, count : Int)

Returns count or less (if there aren't enough) elements starting at the given start index.

Negative indices count backward from the end of the array (-1 is the last element). Additionally, an empty array is returned when the starting index for an element range is at the end of the array.

Raises IndexError if the starting index is out of range.

require "bit_array"

ba = BitArray.new(5)
ba[0] = true; ba[2] = true; ba[4] = true
ba # => BitArray[10101]

ba[-3, 3] # => BitArray[101]
ba[6, 1]  # raise indexError
ba[1, 2]  # => BitArray[01]
ba[5, 1]  # => BitArray[]
View source

#[](range : Range)

Returns all elements that are within the given range.

Negative indices count backward from the end of the array (-1 is the last element). Additionally, an empty array is returned when the starting index for an element range is at the end of the array.

Raises IndexError if the starting index is out of range.

require "bit_array"

ba = BitArray.new(5)
ba[0] = true; ba[2] = true; ba[4] = true
ba # => BitArray[10101]

ba[1..3]    # => BitArray[010]
ba[4..7]    # => BitArray[1]
ba[6..10]   # raise IndexError
ba[5..10]   # => BitArray[]
ba[-2...-1] # => BitArray[0]
View source

#[]=(index, value : Bool)

Sets the bit at the given index. Negative indices can be used to start counting from the end of the array. Raises IndexError if trying to access a bit outside the array's range.

require "bit_array"

ba = BitArray.new(5)
ba[3] = true
View source

#dup

Returns a new BitArray with all of the same elements.

View source

#hash(hasher)

View source

#inspect(io : IO) : Nil

Creates a string representation of self.

require "bit_array"

ba = BitArray.new(5)
ba.to_s # => "BitArray[00000]"
View source

#invert

Inverts all bits in the array. Falses become true and vice versa.

require "bit_array"

ba = BitArray.new(5)
ba[2] = true; ba[3] = true
ba # => BitArray[00110]
ba.invert
ba # => BitArray[11001]
View source

#size : Int32

The number of bits the BitArray stores

View source

#to_s(io : IO) : Nil

Creates a string representation of self.

require "bit_array"

ba = BitArray.new(5)
ba.to_s # => "BitArray[00000]"
View source

#to_slice : Bytes

Returns a Bytes able to read and write bytes from a buffer. The slice will be long enough to hold all the bits groups in bytes despite the UInt32 internal representation. It's useful for reading and writing a bit array from a byte buffer directly.

Warning

It is undefined behaviour to set any of the unused bits of a bit array to true via a slice.

View source

#toggle(index)

Toggles the bit at the given index. A false bit becomes a true bit, and vice versa. Negative indices can be used to start counting from the end of the array. Raises IndexError if trying to access a bit outside the array's range.

require "bit_array"

ba = BitArray.new(5)
ba[3] # => false
ba.toggle(3)
ba[3] # => true
View source

#unsafe_fetch(index : Int)

Returns the element at the given index, without doing any bounds check.

Indexable makes sure to invoke this method with index in 0...size, so converting negative indices to positive ones is not needed here.

Clients never invoke this method directly. Instead, they access elements with #[](index) and #[]?(index).

This method should only be directly invoked if you are absolutely sure the index is in bounds, to avoid a bounds check for a small boost of performance.

View source