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



11
12
13
# File 'lib/ttfunk/bit_field.rb', line 11

def initialize(value = 0)
  @value = value
end

Instance Attribute Details

#valueInteger (readonly)

Serialized value.

Returns:

  • (Integer)


8
9
10
# File 'lib/ttfunk/bit_field.rb', line 8

def value
  @value
end

Instance Method Details

#dupBitField

Get a duplicate of this bit field.

Returns:



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.

Parameters:

  • pos (Integer)


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?

Parameters:

  • pos (Integer)

Returns:

  • (Boolean)


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.

Parameters:

  • pos (Integer)

    bit position



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

def on(pos)
  @value |= 2**pos
end

#on?(pos) ⇒ Boolean

If bit on?

Parameters:

  • pos (Integer)

Returns:

  • (Boolean)


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

def on?(pos)
  (value & (2**pos)).positive?
end