[REBOL] Re: subtracting binaries
From: greggirwin:mindspring at: 3-Apr-2004 10:33
Hi Maarten,
MK> I tested it some more, and it works like a charm! Thanks for the
MK> time-saver. Especially the fact that it works modulo (max value of the
MK> supplied binary) is really nice.
Great! I wasn't sure if I had overlooked something, or if it was what
you wanted. I won't spend time on it now, but eliminating FOR is an
obvious speed trap (~30%):
add-bitsets: func [a b /local c i n carry] [
carry: 0
c: make bitset! length? a
repeat i (length? a) - 1 [
n: i - 1
switch carry + add get-bit a n get-bit b n [
0 []
1 [set-bit c n carry: 0]
2 [carry: 1]
3 [set-bit c n carry: 1]
]
]
if carry <> 0 [
i: 0
while [bit-set? c i][
clear-bit c i
i: i + 1
]
set-bit c i
]
c
]
MK> BTW: I never really "got" bitsets (shame) so if anybody wants to
MK> elaborate on how the various operation on bitsets work... The docs don't
MK> mention the bitset! value at all, except for parse (where I used it).
I just derived my stuff via experimentation. Occasionally I look at
old libraries I've done and port pieces of them. I had an old
bit-vector class I pseudo-ported (haven't done bit-matrix yet :) and
just played around to see how they worked. Things like UNION,
DIFFERENCE, and INTERSECT also work on them, which is nice. Now, if
only we had native SHIFT and ROTATE functions...
-- Gregg