[REBOL] Re: Bitwise operations
From: dockimbel:free at: 15-Aug-2003 3:36
Karl Robillard wrote:
[...]
> Here are some functions I use which would be much faster as natives:
>
> ; Converts integer to 16 bit big-endian binary value.
> bin16: func [n] [
> to-binary reduce [ to-integer n / 256 n and 255 ]
> ]
>
> bin16-le: func [n] [
> to-binary reduce [ n and 255 to-integer n / 256 ]
> ]
>
> ; Converts integer to 32 bit big-endian binary value.
> bin32: func [n] [
> load join "#{" [form to-hex n "}"]
> ]
>
> bin32-le: func [n /local data a b c d] [
> data: form to-hex n
> a: copy/part data 2
> b: copy/part skip data 2 2
> c: copy/part skip data 4 2
> d: copy/part skip data 6 2
> load join "#{" [d c b a "}"]
> ]
>
> swap16: func [data] [ data/1 + (data/2 * 256) ]
>
> swap32: func [data] [ data/1 + (data/2 * 256) + (data/3 * 65536) +
> (data/4 * 16777216) ]
Until RT releases the plugin API so you'll be able to code them in C directly,
here's a faster implementation of these functions :
bin16: func [n][at debase/base to-hex n 16 3]
bin16-le: func [n][copy/part head reverse debase/base to-hex n 16 2]
bin32: func [n][debase/base to-hex n 16]
bin32-le: func [n][head reverse debase/base to-hex n 16]
swap16: swap32: func [data][to integer! head reverse data]
HTH,
-DocKimbel