Class: TTFunk::BitField
- Inherits:
-
Object
- Object
- TTFunk::BitField
- Defined in:
- lib/ttfunk/bit_field.rb
Overview
Bitfield represents a series of bits that can individually be toggled.
Instance Attribute Summary collapse
-
#value ⇒ Integer
readonly
Serialized value.
Instance Method Summary collapse
-
#dup ⇒ BitField
Get a duplicate of this bit field.
-
#initialize(value = 0) ⇒ BitField
constructor
A new instance of BitField.
-
#off(pos) ⇒ void
Set bit off.
-
#off?(pos) ⇒ Boolean
Is bit off?.
-
#on(pos) ⇒ void
Set bit on.
-
#on?(pos) ⇒ Boolean
If bit on?.
Constructor Details
#initialize(value = 0) ⇒ BitField
Returns a new instance of BitField.
11 12 13 |
# File 'lib/ttfunk/bit_field.rb', line 11 def initialize(value = 0) @value = value end |
Instance Attribute Details
#value ⇒ Integer (readonly)
Serialized value.
8 9 10 |
# File 'lib/ttfunk/bit_field.rb', line 8 def value @value end |
Instance Method Details
#dup ⇒ BitField
Get a duplicate of this bit field.
50 51 52 |
# File 'lib/ttfunk/bit_field.rb', line 50 def dup self.class.new(value) end |
#off(pos) ⇒ void
This method returns an undefined value.
Set bit off.
35 36 37 |
# File 'lib/ttfunk/bit_field.rb', line 35 def off(pos) @value &= (2**Math.log2(value).ceil) - (2**pos) - 1 end |
#off?(pos) ⇒ Boolean
Is bit off?
43 44 45 |
# File 'lib/ttfunk/bit_field.rb', line 43 def off?(pos) !on?(pos) end |
#on(pos) ⇒ void
This method returns an undefined value.
Set bit on.
19 20 21 |
# File 'lib/ttfunk/bit_field.rb', line 19 def on(pos) @value |= 2**pos end |
#on?(pos) ⇒ Boolean
If bit on?
27 28 29 |
# File 'lib/ttfunk/bit_field.rb', line 27 def on?(pos) (value & (2**pos)).positive? end |