Class: Bitsy::Mask

Inherits:
Object
  • Object
show all
Defined in:
lib/bitsy/mask.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flag, value) ⇒ Mask

Returns a new instance of Mask.



10
11
12
13
# File 'lib/bitsy/mask.rb', line 10

def initialize(flag, value)
  @flag = flag
  @value = value
end

Instance Attribute Details

#flagObject (readonly)

Returns the value of attribute flag.



4
5
6
# File 'lib/bitsy/mask.rb', line 4

def flag
  @flag
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/bitsy/mask.rb', line 4

def value
  @value
end

Class Method Details

.with_index(flag, idx) ⇒ Object



6
7
8
# File 'lib/bitsy/mask.rb', line 6

def self.with_index(flag, idx)
  self.new(flag, (1 << idx))
end

Instance Method Details

#&(other) ⇒ Object



19
20
21
# File 'lib/bitsy/mask.rb', line 19

def &(other)
  self.class.new(combine_flag(other, 'AND'), value & other.to_i)
end

#^(other) ⇒ Object



27
28
29
# File 'lib/bitsy/mask.rb', line 27

def ^(other)
  self.class.new(combine_flag(other, 'XOR'), value ^ other.to_i)
end

#to_iObject



15
16
17
# File 'lib/bitsy/mask.rb', line 15

def to_i
  value
end

#|(other) ⇒ Object



23
24
25
# File 'lib/bitsy/mask.rb', line 23

def |(other)
  self.class.new(combine_flag(other, 'OR'), value | other.to_i)
end

#~Object



31
32
33
# File 'lib/bitsy/mask.rb', line 31

def ~
  self.class.new("NOT_#{flag}".to_sym, ~value)
end