Hi Gregg ,

I tested it some more, and it works like a charm! Thanks for the 
time-saver. Especially the fact that it works modulo (max value of the 
supplied binary) is really nice.

And getting it back to a binary is simple:

 >> a: add-bitsets mb checksum/secure mold now mb checksum/secure mold 
now/precise
== make bitset! #{A2B6BECF9C1DC2F6C8F35FAD01DD46BF6545EDB4}
 >> make binary! a
== #{
A2B6BECF9C1DC2F6C8F35FAD01DD46BF6545EDB4000000000000000000000000
}
 >> copy/part make binary! a 20
== #{A2B6BECF9C1DC2F6C8F35FAD01DD46BF6545EDB4}

BTW: I never really "got" bitsets (shame) so if anybody wants to 
elaborate on how the various operation on bitsets work... The docs don't 
mention the bitset! value at all, except for parse (where I used it).

Thanks!

--Maarten

Gregg Irwin wrote:

> Hi Maarten,
> 
> I know I have other things to be doing :), but this kept coming up in my
> mind...wondering if doing binary math on bitsets would work.
> 
> The following is very much a proof-of-concept, but it might be an idea
> worth pursuing.
> 
> -- Gregg
> 
> ----------------------------------------------------------------
> 
> get-bit:   func [bitset index "0 to n-1"] [either find bitset index [1][0]]
> set-bit:   func [bitset index "0 to n-1"] [insert bitset index]
> bit-set?:  func [bitset index "0 to n-1"] [found? find bitset index]
> clear-bit: func [bitset index "0 to n-1"] [remove/part bitset index]
> 
> add-bitsets: func [a b /local c i n carry] [
>     carry: 0
>     c: make bitset! length? a
>     for i 0 (length? a) - 1 1 [
>         n: add  get-bit a i  get-bit b i
>         ;print [get-bit a i  get-bit b i  n]
>         switch n + carry [
>             0 []
>             1 [set-bit c i  carry: 0]
>             2 [carry: 1]
>             3 [set-bit c i  carry: 1]
>         ]
>         ;repeat i length? c [prin form get-bit c i - 1] print [tab carry]
>     ]
>     i: 0
>     if carry <> 0 [
>         while [bit-set? c i][
>             clear-bit c i
>             i: i + 1
>         ]
>         set-bit c i
>     ]
>     c
> ]
> 
> subtract-bitsets: func [a b] [
>     if a = b [return make bitset! length? a]
>     add-bitsets a complement b
> ]
> 
> mb: func [val][make bitset! val]
> 
> add-bitsets mb #{00} mb #{00}
> add-bitsets mb #{01} mb #{02}
> add-bitsets mb #{F0} mb #{0F}
> add-bitsets mb #{FF} mb #{FF}
> add-bitsets mb #{FF} mb #{01}
> 
> subtract-bitsets mb #{00} mb #{00}
> subtract-bitsets mb #{02} mb #{01}
> subtract-bitsets mb #{FF} mb #{0F}
> subtract-bitsets mb #{FF} mb #{FF}
> subtract-bitsets mb #{00} mb #{01}
> subtract-bitsets mb #{00} mb #{FF}
> 

-- 
To unsubscribe from this list, just send an email to
[EMAIL PROTECTED] with unsubscribe as the subject.

Reply via email to