Class: Integer

Inherits:
Object show all
Defined in:
lib/mug/bittest.rb

Instance Method Summary collapse

Instance Method Details

#and?(other, test: :any) ⇒ Boolean

Tests common bits in this AND other.

test:

:any  => true if any bits are set
:all  => true if all bits are set

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
# File 'lib/mug/bittest.rb', line 11

def and? other, test: :any
  case test.to_sym
  when :any
    and_any? other
  when :all
    and_all? other
  else
    raise ArgumentError, "invalid value for 'test' (given #{test.inspect}, should be :any or :all)"
  end
end

#and_all?(other) ⇒ Boolean

True if this AND other is other.

i.e. if all set bits in other are set in this.

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/mug/bittest.rb', line 37

def and_all? other
  return false if other.zero?
  self & other == other
end

#and_any?(other) ⇒ Boolean

True if this AND other is non-zero.

i.e. if any set bits in other are set in this.

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/mug/bittest.rb', line 27

def and_any? other
  return false if other.zero?
  self & other != 0
end

#or?(other) ⇒ Boolean

True if this OR other is non-zero.

Returns:

  • (Boolean)


45
46
47
# File 'lib/mug/bittest.rb', line 45

def or? other
  self | other != 0
end

#xor?(other) ⇒ Boolean

True if this XOR other is non-zero.

Returns:

  • (Boolean)


52
53
54
# File 'lib/mug/bittest.rb', line 52

def xor? other
  self ^ other != 0
end