Sha256: ae77273842cd69d2efe69217b850224a3006fcc5fcd29c91cad6a0c13b77aaf0

Contents?: true

Size: 566 Bytes

Versions: 4

Compression:

Stored size: 566 Bytes

Contents

unless Bignum.method_defined? :bit_length
  require 'backports/2.0.0/range/bsearch'
  class Bignum
    def bit_length
      # We use the fact that bignums use the minimum number of "words" necessary
      # where "words" is some number of bytes <= to the size of a fixnum
      # So we have (size - word_size) * 8 < bit_length <= size * 8
      n = 8 * (size - 42.size)
      smaller = self >> n
      if smaller >= 0
        smaller += 1
      else
        smaller = -smaller
      end
      n + (1..8 * 42.size).bsearch{|i| smaller <= (1 << i) }
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/backports-3.6.8/lib/backports/2.1.0/bignum/bit_length.rb
backports-3.6.8 lib/backports/2.1.0/bignum/bit_length.rb
backports-3.6.7 lib/backports/2.1.0/bignum/bit_length.rb
backports-3.6.6 lib/backports/2.1.0/bignum/bit_length.rb