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