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.
Source Code
lib/ttfunk/bit_field.rb, line 11
11 | def initialize(value = 0) |
12 | @value = value |
13 | end
|
Instance Attribute Details
#value ⇒ Integer (readonly)
Serialized value.
Source Code
lib/ttfunk/bit_field.rb, line 8
8 | def value |
9 | @value
|
10 | end
|
Instance Method Details
#dup ⇒ BitField
Get a duplicate of this bit field.
Source Code
lib/ttfunk/bit_field.rb, line 50
50 | def dup |
51 | self.class.new(value) |
52 | end
|
#off(pos) ⇒ void
This method returns an undefined value.
Set bit off.
Source Code
lib/ttfunk/bit_field.rb, line 35
35 | def off(pos) |
36 | @value &= (2**Math.log2(value).ceil) - (2**pos) - 1 |
37 | end
|
#off?(pos) ⇒ Boolean
Is bit off?
Source Code
lib/ttfunk/bit_field.rb, line 43
43 | def off?(pos) |
44 | !on?(pos) |
45 | end
|
#on(pos) ⇒ void
This method returns an undefined value.
Set bit on.
Source Code
lib/ttfunk/bit_field.rb, line 19
19 | def on(pos) |
20 | @value |= 2**pos |
21 | end
|
#on?(pos) ⇒ Boolean
If bit on?
Source Code
lib/ttfunk/bit_field.rb, line 27
27 | def on?(pos) |
28 | (value & (2**pos)).positive? |
29 | end
|