class TTFunk::BitField

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

Attributes

value[R]

Serialized value. @return [Integer]

Public Class Methods

new(value = 0) click to toggle source

@param value [Integer] initial value

# File lib/ttfunk/bit_field.rb, line 11
def initialize(value = 0)
  @value = value
end

Public Instance Methods

dup() click to toggle source

Get a duplicate of this bit field.

@return [BitField]

# File lib/ttfunk/bit_field.rb, line 50
def dup
  self.class.new(value)
end
off(pos) click to toggle source

Set bit off.

@param pos [Integer] @return [void]

# File lib/ttfunk/bit_field.rb, line 35
def off(pos)
  @value &= (2**Math.log2(value).ceil) - (2**pos) - 1
end
off?(pos) click to toggle source

Is bit off?

@param pos [Integer] @return [Boolean]

# File lib/ttfunk/bit_field.rb, line 43
def off?(pos)
  !on?(pos)
end
on(pos) click to toggle source

Set bit on.

@param pos [Integer] bit position @return [void]

# File lib/ttfunk/bit_field.rb, line 19
def on(pos)
  @value |= 2**pos
end
on?(pos) click to toggle source

If bit on?

@param pos [Integer] @return [Boolean]

# File lib/ttfunk/bit_field.rb, line 27
def on?(pos)
  (value & (2**pos)).positive?
end