Skip to content

Commit

Permalink
use bit trick to test for power of 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Kolodin committed Dec 4, 2015
1 parent 6a256aa commit fd04cb4
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/bitfields.rb
Expand Up @@ -30,7 +30,7 @@ def inherited(klass)
def self.extract_bits(options)
bitfields = {}
options.keys.select{|key| key.is_a?(Numeric) }.each do |bit|
raise "#{bit} is not a power of 2 !!" unless bit.to_s(2).scan('1').size == 1
raise "#{bit} is not a power of 2 !!" unless bit & (bit - 1) == 0
bit_name = options.delete(bit).to_sym
raise DuplicateBitNameError if bitfields.include?(bit_name)
bitfields[bit_name] = bit
Expand Down

0 comments on commit fd04cb4

Please sign in to comment.