Class: Numeric

Inherits:
Object show all
Defined in:
lib/origen/core_ext/numeric.rb

Instance Method Summary collapse

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_binObject



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_hexObject



6
7
8
# File 'lib/origen/core_ext/numeric.rb', line 6

def to_hex
  "0x#{to_s(16).upcase}"
end

#undefined?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/origen/core_ext/numeric.rb', line 2

def undefined?
  false
end