[REBOL] Re: bit shifts
From: greggirwin:starband at: 20-Sep-2001 6:45
Well, this mostly works, but wheras the integer value 1 right-shifted
becomes 0, the same value divided by 2 becomes 0,5. Even if one should
round the value (how do we do so in rebol, by the way?), it would become 1,
not 0. So I think it's better to shift "à proprement dire".
We're not dividing by 2 but by (2 ** n).
Actual function definitions:
shl: func [value shift-count] [to-integer (value * (2 ** shift-count))]
shr: func [value shift-count] [to-integer (value / (2 ** shift-count))]
I just did some quick tests and these seem to work just fine. Note that if
you remove the to-integer calls, so they can go beyond 30 bit-shifts, then
you can get decimal results. REBOL doesn't seem to have an integer-divide
operator, so we'd have to do that ourselves. If you're only concerned about
shifting too far to the right, you could do this:
shr: func [value shift-count /local result] [
result: (value / (2 ** shift-count))
either result >= 1 [
You can get bogus results by passing in a number that is not a power of
2 and shifting that (e.g. shr 25 2), so you have to decide what you want
to do in that case.
<< Your other routines seem to work, but only partially, I'm afraid. It
rebol has great problems with the debase function... But thanks anyway. >>
Hmmm. I'll have to check it out. Thanks for letting me know.