Class: Numeric
Instance Method Summary collapse
-
#reverse_bits(width) ⇒ Object
Reverses the bit representation of a number and returns the new value.
- #to_bin ⇒ Object
-
#to_bitstring(width) ⇒ Object
Converts a number to a String representing binary number Requires width of bit string for padding.
- #to_hex ⇒ Object
Instance Method Details
#reverse_bits(width) ⇒ Object
Reverses the bit representation of a number and returns the new value. Useful when changing register data based on bit order
20 21 22 23 24 25 26 |
# File 'lib/origen/core_ext/numeric.rb', line 20 def reverse_bits(width) result = 0 0.upto(width - 1) do |i| result += self[i] * 2**(width - 1 - i) end result end |
#to_bin ⇒ Object
6 7 8 |
# File 'lib/origen/core_ext/numeric.rb', line 6 def to_bin "0b#{to_s(2)}" end |
#to_bitstring(width) ⇒ Object
Converts a number to a String representing binary number Requires width of bit string for padding. If the width is less than the number of bits required to represent the number the width argument is meaningless.
14 15 16 |
# File 'lib/origen/core_ext/numeric.rb', line 14 def to_bitstring(width) '%0*b' % [width, self] end |
#to_hex ⇒ Object
2 3 4 |
# File 'lib/origen/core_ext/numeric.rb', line 2 def to_hex "0x#{to_s(16).upcase}" end |