Class: Integer
- Inherits:
-
Object
- Object
- Integer
- Defined in:
- lib/ce-greenbutton/ruby_extensions.rb
Overview
Extending the Integer to add the utility method of extracting bits
Instance Method Summary collapse
-
#bits(options) ⇒ Object
Extracts bits in range from: to to:.
Instance Method Details
#bits(options) ⇒ Object
Extracts bits in range from: to to:
Example:
10.bits(from:1, to:2) # 1010
# => 1
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ce-greenbutton/ruby_extensions.rb', line 10 def bits() from = [:from] || 0 to = [:to] val = self unless from.nil? val = val >> from end unless to.nil? val = val & (2 ** (to - from + 1) - 1) end val end |