Method: Integer#split

Defined in:
lib/ruby-vpi/integer.rb

#split(aWidth = 8) ⇒ Object

Splits this integer into an array of smaller integers, each of which have the given positive, non-zero width (number of bits). These smaller integers are ordered from left to right, in the same way that humans write unsigned binary numbers; for example:

>> 6.split 1

> [1, 1, 0]

>> 6.split(1).map {|i| i.to_s 2}

> [“1”, “1”, “0”]

>> 6.split 2

> [1, 2]

>> 6.split(2).map {|i| i.to_s 2}

> [“1”, “10”]

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ruby-vpi/integer.rb', line 121

def split aWidth = 8
  raise ArgumentError, "width must be positive and non-zero" unless aWidth > 0

  int, bits = self, length
  mask = aWidth.to_mask
  words = []

  while bits > 0
    words.unshift int & mask
    int >>= aWidth
    bits -= aWidth
  end

  words
end