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
- #undefined? ⇒ Boolean
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
24 25 26 27 28 29 30 |
# File 'lib/origen/core_ext/numeric.rb', line 24 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
10 11 12 |
# File 'lib/origen/core_ext/numeric.rb', line 10 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.
18 19 20 |
# File 'lib/origen/core_ext/numeric.rb', line 18 def to_bitstring(width) '%0*b' % [width, self] end |
#to_hex ⇒ Object
6 7 8 |
# File 'lib/origen/core_ext/numeric.rb', line 6 def to_hex "0x#{to_s(16).upcase}" end |
#undefined? ⇒ Boolean
2 3 4 |
# File 'lib/origen/core_ext/numeric.rb', line 2 def undefined? false end |