summaryrefslogtreecommitdiff
path: root/lib/ipaddress/extensions/extensions.rb
blob: a474e184217e278275f932ccf9bf570f2e2f9590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class << Math
  def log2(n); log(n) / log(2); end
end

if RUBY_VERSION =~ /1\.8/
    class Hash
        alias :key :index
    end
end

class Integer
  def power_of_2?
    Math::log2(self).to_i == Math::log2(self)
  end

  def closest_power_of_2(limit=32)
    self.upto(limit) do |i|
      return i if i.power_of_2?
    end
  end
end