On this page:
define-bits
8.1 Implementation
n-bit-mask
bits-join
bits-split-list
n-bit-sextend
bits-split
9.2

8 Bits🔗

 (require ledum/define/bits) package: ledum-definers

This module and its private implementation allow for manipulating bit ranges of larger integers.

syntax

(define-bits maybe-provide bit-bindings maybe-more
  #:from bit-bindings)
 
maybe-provide = 
  | #:provide
     
bit-bindings = (id bits) ...
     
maybe-more = 
  | #:or bit-bindings maybe-more
This form binds all the id identifiers on the left-hand side of the #:from to refer to given slices of id identifier values on the right-hand side when interpreted as bits bit-wide numbers. Each id on the left-hand side slices bits bits from the right-hand side. If the slices are mutated using set! the change modifies the underlying right-hand side binding as well.

If the #:provide keyword is used, all the id bindings on the left-hand side are provided.

If id on the left-hand side is _, no binding for that slice is created - but it is still part of the sliced structure.

Multiple sets of left-hand pairs of identifier and bit width can be specified using the #:or keyword.

Examples:
; Basic usage
> (define flags 10)
> (define-bits (lvl 2) (cf 1) (zf 1) #:from (flags 4))
> lvl

2

> cf

1

> zf

0

; Multiple right-hand side sources can be specified
> (define more-flags 255)
> (define-bits (rel -6) (lvl 4) (cf 1) (zf 1) #:from (more-flags 8) (flags 4))
> rel

-1

> lvl

14

> cf

1

> zf

0

; Slices can be mutated
> (set! rel 0)
> more-flags

3

; Multiple left-hand sides are allowed
> (define-bits (upper 8) (lower 8) #:or (a 4) (b 8) (c 4)
    #:from (more-flags 8) (flags 4))
> upper

0

> lower

58

> (set! b 255)
> upper

15

> lower

250

8.1 Implementation🔗

 (require ledum/define/bits/impl) package: ledum-definers

Utility functions.

Computes bit mask for n-bit integer which is also the maximal value for such unsigned integer.

Examples:
> (n-bit-mask 4)

15

> (n-bit-mask 12)

4095

procedure

(bits-join valen ...)  exact-integer?

  valen : (list/c exact-integer? exact-positive-integer?)
Each valen contains a value and bit width. The arguments are in big-endian order. The result is a composite integer with all the values concatenated into one.

Examples:
> (bits-join '(15 4) '(1 4))

241

> (bits-join '(1 2) '(3 4) '(5 6))

1221

procedure

(bits-split-list value len ...)  (listof exact-integer?)

  value : exact-integer?
  len : exact-positive-integer?
Splits given value into a number of parts based on the bit-widths as specified by each of the len bit-widths.

Example:
> (bits-split-list 4660 4 8 4)

'(1 35 4)

procedure

(n-bit-sextend numbits value)  exact-integer?

  numbits : exact-positive-integer?
  value : exact-integer?
Performs a sign extension of given value as if it is a numbits wide two-complement integer.

Examples:
> (n-bit-sextend 8 254)

-2

> (n-bit-sextend 8 127)

127

> (n-bit-sextend 8 128)

-128

procedure

(bits-split value len ...)  any

  value : exact-integer?
  len : exact-positive-integer?
Like bits-split-list but returns values that can be bound instead.

Examples:
> (define-values (a b) (bits-split 241 4 4))
> a

15

> b

1