Class: Bignum

Inherits:
Object show all
Defined in:
lib/backports/2.4.0/bignum/dup.rb,
lib/backports/2.1.0/bignum/bit_length.rb

Instance Method Summary collapse

Instance Method Details

#bit_lengthObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/backports/2.1.0/bignum/bit_length.rb', line 4

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

#dupObject



2
3
4
# File 'lib/backports/2.4.0/bignum/dup.rb', line 2

def dup
  self
end