Method: BitMagic::BitField#read_bits

Defined in:
lib/bit_magic/bit_field.rb

#read_bits(*args) ⇒ Hash

Read the specified bit indices into a hash with bit index as key

Examples:

Read a list of bits into a hash

bit_field = BitField.new(5)
bit_field.read_bits(0, 1, 2)
#=> {0=>1, 1=>0, 2=>1}
# because 5 is 101 in binary

Parameters:

  • bits (Integer)

    one or more bit indices to read.

Returns:

  • (Hash)

    a hash with the bit index as key and bit (1 or 0) as value



35
36
37
38
39
# File 'lib/bit_magic/bit_field.rb', line 35

def read_bits(*args)
  {}.tap do |m|
    args.each { |bit| m[bit] = @value[bit] }
  end
end