Class: Bits
- Inherits:
-
Object
- Object
- Bits
- 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
- #+(bits) ⇒ Object
- #bit_string ⇒ Object
- #hex_string ⇒ Object
-
#initialize(bits, length = 0) ⇒ Bits
constructor
bits
- The bit field to store
length
-
Length of the bit field - default to calculated length, 0-pad on MSB.
- The bit field to store
- #integer ⇒ Object
- #oct_string ⇒ Object
- #set(bits) ⇒ Object
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_string ⇒ Object
19 20 21 |
# File 'lib/bits.rb', line 19 def bit_string @bits end |
#hex_string ⇒ Object
23 24 25 |
# File 'lib/bits.rb', line 23 def hex_string @bits.to_i(2).to_s(16) end |
#integer ⇒ Object
31 32 33 |
# File 'lib/bits.rb', line 31 def integer @bits.to_i(2) end |
#oct_string ⇒ Object
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 |