P
Pito Salas
What I want to do is to extract the integer value corresponding to a bit
field within a FixNum. That is, for example, what is the integer value
formed by bits 4,5,6,7,8 of a number?
This is my ugly first cut. There must be a library function that does
this but I can't find it. Anyone have a pointer, that would be great!!
Or a suggestion on refactoring/rewriting this?
Thanks!
def bit_field(from, to, val)
accum = 0
ind = 0
from.upto(to) do |index|
bitval = val[index]
accum = accum + bitval * (2 ** ind)
ind = ind + 1
end
accum
end
field within a FixNum. That is, for example, what is the integer value
formed by bits 4,5,6,7,8 of a number?
This is my ugly first cut. There must be a library function that does
this but I can't find it. Anyone have a pointer, that would be great!!
Or a suggestion on refactoring/rewriting this?
Thanks!
def bit_field(from, to, val)
accum = 0
ind = 0
from.upto(to) do |index|
bitval = val[index]
accum = accum + bitval * (2 ** ind)
ind = ind + 1
end
accum
end