Sha256: 1174ccd9c0d6d7d6002eb7bc58921ee7e6cf8db6e79e95ce6f667ed80c63a0a4

Contents?: true

Size: 531 Bytes

Versions: 4

Compression:

Stored size: 531 Bytes

Contents

require 'aruba/aruba_path'

# Aruba
module Aruba
  # Contracts
  module Contracts
    # Is value power of two
    class IsPowerOfTwo
      # Check value
      #
      # @param [Integer] value
      #   The value to be checked
      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.positive? && (value & (value - 1)).zero?
      rescue StandardError
        false
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
aruba-1.0.4 lib/aruba/contracts/is_power_of_two.rb
aruba-1.0.3 lib/aruba/contracts/is_power_of_two.rb
aruba-1.0.2 lib/aruba/contracts/is_power_of_two.rb
aruba-1.0.1 lib/aruba/contracts/is_power_of_two.rb