2009-02-20 21:14:54 -05:00
|
|
|
! Copyright (C) 2009 Daniel Ehrenberg
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: sequences kernel math accessors sequences.private ;
|
|
|
|
IN: math.bits
|
|
|
|
|
|
|
|
TUPLE: bits { number read-only } { length read-only } ;
|
|
|
|
C: <bits> bits
|
|
|
|
|
|
|
|
: make-bits ( number -- bits )
|
2009-08-11 19:15:53 -04:00
|
|
|
[ T{ bits f 0 0 } ] [ dup abs log2 1 + <bits> ] if-zero ; inline
|
2009-02-20 21:14:54 -05:00
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
M: bits length length>> ; inline
|
2009-02-20 21:14:54 -05:00
|
|
|
|
2009-08-17 23:32:21 -04:00
|
|
|
M: bits nth-unsafe number>> swap bit? ; inline
|
2009-02-20 21:14:54 -05:00
|
|
|
|
|
|
|
INSTANCE: bits immutable-sequence
|
2009-06-01 23:39:02 -04:00
|
|
|
|
|
|
|
: unbits ( seq -- number )
|
2009-08-13 20:21:44 -04:00
|
|
|
<reversed> 0 [ [ 1 shift ] dip [ 1 + ] when ] reduce ;
|