]> gitweb.factorcode.org Git - factor.git/blob - core/io/binary/binary.factor
use radix literals
[factor.git] / core / io / binary / binary.factor
1 ! Copyright (C) 2003, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math sequences ;
4 IN: io.binary
5
6 : le> ( seq -- x ) dup length iota 0 [ 8 * shift + ] 2reduce ;
7 : be> ( seq -- x ) 0 [ [ 8 shift ] dip + ] reduce ;
8
9 : mask-byte ( x -- y ) 0xff bitand ; inline
10
11 : nth-byte ( x n -- b ) -8 * shift mask-byte ; inline
12
13 : >le ( x n -- byte-array ) iota [ nth-byte ] with B{ } map-as ;
14 : >be ( x n -- byte-array ) >le reverse! ;
15
16 : d>w/w ( d -- w1 w2 )
17     [ 0xffffffff bitand ]
18     [ -32 shift 0xffffffff bitand ] bi ;
19
20 : w>h/h ( w -- h1 h2 )
21     [ 0xffff bitand ]
22     [ -16 shift 0xffff bitand ] bi ;
23
24 : h>b/b ( h -- b1 b2 )
25     [ mask-byte ]
26     [ -8 shift mask-byte ] bi ;
27
28 : signed-le> ( bytes -- x )
29     [ le> ] [ length 8 * 1 - 2^ 1 - ] bi
30     2dup > [ bitnot bitor ] [ drop ] if ;
31
32 : signed-be> ( bytes -- x )
33     <reversed> signed-le> ;