Class: TTFunk::BitField

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

Overview

Bitfield represents a series of bits that can individually be toggled.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = 0) ⇒ BitField

Returns a new instance of BitField.

Parameters:

  • value (Integer) (defaults to: 0)

    initial value

Source Code
lib/ttfunk/bit_field.rb, line 11
11
def initialize(value = 0)
12
  @value = value
13
end

Instance Attribute Details

#valueInteger (readonly)

Serialized value.

Returns:

  • (Integer)
Source Code
lib/ttfunk/bit_field.rb, line 8
8
def value
9
  @value
10
end

Instance Method Details

#dupBitField

Get a duplicate of this bit field.

Returns:

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.

Parameters:

  • pos (Integer)
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?

Parameters:

  • pos (Integer)

Returns:

  • (Boolean)
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.

Parameters:

  • pos (Integer)

    bit position

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?

Parameters:

  • pos (Integer)

Returns:

  • (Boolean)
Source Code
lib/ttfunk/bit_field.rb, line 27
27
def on?(pos)
28
  (value & (2**pos)).positive?
29
end