Class: Bits

Inherits:
Object
  • Object
show all
Defined in:
lib/bits.rb

Overview

Bits provides an easy way to store and access sequences of bits, allowing input and output in multiple formats

Instance Method Summary collapse

Constructor Details

#initialize(bits, length = 0) ⇒ Bits

bits

The bit field to store

length

Length of the bit field - default to calculated length, 0-pad on

MSB



10
11
12
13
# File 'lib/bits.rb', line 10

def initialize(bits, length = 0)
  @length = length
  @bits = bit_string_from_input(bits, length)
end

Instance Method Details

#+(bits) ⇒ Object



15
16
17
# File 'lib/bits.rb', line 15

def +(bits)
  Bits.new("0b#{@bits}" + bit_string_from_input(bits))
end

#bit_stringObject



19
20
21
# File 'lib/bits.rb', line 19

def bit_string
  @bits
end

#hex_stringObject



23
24
25
# File 'lib/bits.rb', line 23

def hex_string
  @bits.to_i(2).to_s(16)
end

#integerObject



31
32
33
# File 'lib/bits.rb', line 31

def integer
  @bits.to_i(2)
end

#oct_stringObject



27
28
29
# File 'lib/bits.rb', line 27

def oct_string
  @bits.to_i(2).to_s(8)
end

#set(bits) ⇒ Object



35
36
37
# File 'lib/bits.rb', line 35

def set(bits)
  @bits = bit_string_from_input(bits, @length)
end