Method: BitMagic::BitField#read_field

Defined in:
lib/bit_magic/bit_field.rb

#read_field(*args) ⇒ Integer Also known as: []

Read the specified bit indices as a group, in the order given

Examples:

Read bits or a list of bits into an integer

bit_field = BitField.new(101) # 1100101 in binary, lsb on the right
bit_field.read_field(0, 1, 2) #=> 5 # or 101
bit_field.read_field(0) #= 1
bit_field.read_field( (2..6).to_a ) #=> 25 # or 11001


52
53
54
55
56
57
58
59
60
# File 'lib/bit_magic/bit_field.rb', line 52

def read_field(*args)
  m = 0
  args.flatten.each_with_index do |bit, i|
    if bit.is_a?(Integer)
      m |= ((@value[bit] || 0) << i)
    end
  end
  m
end