byte-reverse ( n signed? -- quot )
Alien endian-aware types
Next:be8


Vocabulary
alien.endian

Inputs and outputs
nan integer
signed?a boolean
quota quotation


Word description
Reverses the n bytes in an integer with bitwise operations. The second parameter only works for 1, 2, 4, or 8 byte signed numbers.

Definition
USING: combinators generalizations kernel macros math sequences
;

IN: alien.endian

MACRO: byte-reverse ( n signed? -- quot )
[
drop [
dup iota [
[ 1 + - -8 * ] [ nip 8 * ] 2bi
[ [ shift 255 bitand ] curry ] dip
[ shift ] curry compose
] with map
] [ 1 - [ bitor ] n*quot ] bi
] [ [ convert-signed-quot ] [ drop [ ] ] if ] 2bi
[ [ [ cleave ] curry ] dip compose ] dip compose ;