Module: Utilities::Numbers

Defined in:
lib/utilities/numbers.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) get_bits(number)



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/utilities/numbers.rb', line 3

def self.get_bits(number)
  # created by Jim Tucker, 20161012
  # parameter is decimal number representing a bit map
  # returns an array of bits set (power of 2) starting at 0, e.g., [0, 3, 22]
  # use to decode StatusFlags in SF.tblTaxa
  ret_val = []
  counter = 0

  until number == 0
    ret_val << counter unless (number & 1) == 0 # << = push
    counter += 1
    number >>= 1 # >>= = right shift
  end

  ret_val
end