6 Short Bitvectors
(require binaryio/bitvector) | package: binaryio-lib |
Added in version 1.2 of package binaryio-lib.
(+ L (* b_0 (arithmetic-shift 1 (+ 0 SBV-LENGTH-BITS))) (* b_1 (arithmetic-shift 1 (+ 1 SBV-LENGTH-BITS))) (* b_2 (arithmetic-shift 1 (+ 2 SBV-LENGTH-BITS))) ... (* b_L-1 (arithmetic-shift 1 (+ L-1 SBV-LENGTH-BITS))))
Consequently, bitvectors up to about 46 bits are represented using fixnums (on 64-bit versions of Racket), and only bitvectors up to (sub1 (expt 2 SBV-LENGTH-BITS)) bits are representable.
value
value
SBV-LENGTH-BOUND : exact-nonnegative-integer? = 65536
Equivalent to exact-nonnegative-integer?. See also canonical-sbv?.
procedure
(canonical-sbv? v) → boolean?
v : any/c
For example, (make-sbv #b1011 2) is not canonical because it has a bit set after the first two bits.
Warning: In general, the functions in this library may produce bad results if given non-canonical bitvector values.
procedure
le-bits : exact-nonnegative-integer? bitlength : exact-nonnegative-integer?
If le-bits has a bit set after the first bitlength bits, then the result is non-canonical (see canonical-sbv?). If bitlength is not less then SBV-LENGTH-BOUND, an error is raised.
> (sbv->string (make-sbv 6 3)) "011"
procedure
(make-be-sbv be-bits bitlength) → sbv?
be-bits : exact-nonnegative-integer? bitlength : exact-nonnegative-integer?
Equivalent to (sbv-reverse (make-sbv be-bits bitlength)).
> (sbv->string (make-be-sbv 6 3)) "110"
procedure
(sbv-empty? sbv) → boolean?
sbv : sbv?
procedure
(sbv-length sbv) → exact-nonnegative-integer?
sbv : sbv?
procedure
(sbv-bits sbv) → exact-nonnegative-integer?
sbv : sbv?
> (sbv-bits (string->sbv "1011")) 13
procedure
(sbv-bit-field sbv start end) → exact-nonnegative-integer?
sbv : sbv? start : exact-nonnegative-integer? end : exact-nonnegative-integer?
If end is greater than (sbv-length sbv), then the “out of range” bits are set to zero.
> (sbv-bit-field (string->sbv "11100") 1 4) 3
> (sbv-bit-field (string->sbv "11100") 1 10) 3
procedure
sbv : sbv? start : exact-nonnegative-integer? end : exact-nonnegative-integer?
If end is greater than (sbv-length sbv), then the “out of range” bits are set to zero.
> (sbv->string (sbv-slice (string->sbv "11100") 1 4)) "110"
> (sbv->string (sbv-slice (string->sbv "11100") 1 10)) "110000000"
procedure
sbv : sbv? lshift : exact-integer?
> (sbv->string (sbv-shift (string->sbv "11100") 3)) "00011100"
> (sbv->string (sbv-shift (string->sbv "11100") -2)) "100"
procedure
(sbv-bit-set? sbv index) → boolean?
sbv : sbv? index : exact-nonnegative-integer?
> (sbv-bit-set? (string->sbv "1101") 0) #t
> (sbv-bit-set? (string->sbv "1101") 1) #t
> (sbv-bit-set? (string->sbv "1101") 2) #f
procedure
sbv : sbv? index : exact-nonnegative-integer?
> (sbv-ref (string->sbv "1101") 0) 1
> (sbv-ref (string->sbv "1101") 1) 1
> (sbv-ref (string->sbv "1101") 2) 0
Equivalent to (sbv-ref sbv 0) and (sbv-shift sbv -1).
> (sbv-car (string->sbv "110")) 1
> (sbv->string (sbv-cdr (string->sbv "110"))) "10"
procedure
(sbv-append sbv ...) → sbv?
sbv : sbv?
> (sbv->string (sbv-append (string->sbv "1011") (string->sbv "00"))) "101100"
Equivalent to (sbv-append (make-sbv bit 1) sbv).
procedure
(sbv-reverse sbv) → sbv?
sbv : sbv?
> (sbv->string (sbv-reverse (string->sbv "1101"))) "1011"
procedure
(sbv-prefix? sbv1 sbv2) → boolean?
sbv1 : sbv? sbv2 : sbv?
> (sbv-prefix? (string->sbv "110") (string->sbv "110")) #t
> (sbv-prefix? (string->sbv "110") (string->sbv "1100")) #t
> (sbv-prefix? (string->sbv "110") (string->sbv "100110")) #f
> (sbv-prefix? (string->sbv "110") (string->sbv "11")) #f
procedure
(sbv->string sbv) → string?
sbv : sbv?
> (sbv->string (sbv-append (make-sbv 1 1) (make-sbv 1 1) (make-sbv 0 1))) "110"
(make-be-sbv (string->number (sbv->string sbv) 2) (sbv-length sbv))
procedure
(string->sbv s) → sbv?
s : (and/c string? #rx"^[01]*$")