Class: Aruba::Contracts::IsPowerOfTwo

Inherits:
Object
  • Object
show all
Defined in:
lib/aruba/contracts/is_power_of_two.rb

Overview

Is value power of two

Class Method Summary collapse

Class Method Details

.valid?(value) ⇒ Boolean

Check value

Parameters:

  • value (Integer)

    The value to be checked

Returns:

  • (Boolean)

13
14
15
16
17
18
19
# File 'lib/aruba/contracts/is_power_of_two.rb', line 13

def self.valid?(value)
  # explanation for algorithm can be found here:
  # http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/
  value != 0 && (value & (value - 1)) == 0 ? true : false
rescue
  false
end