On this page:
output-bitport?
open-output-bitport
output-bitport-partial
output-bitport-write-bit
output-bitport-write-sbv
output-bitport-get-output
output-bitport-pad
bytes-bit-set?
8.12

7 Bitports🔗ℹ

Added in version 1.2 of package binaryio-lib.

An output bitport is like an output string port (open-output-bytes), except that instead of accumulating bytes, it accumulates bits and packs them into a byte string.

procedure

(output-bitport? v)  boolean?

  v : any/c
Returns #t if v is an output bitport, #f otherwise.

procedure

(open-output-bitport [msf?])  output-bitport?

  msf? : boolean? = #t
Creates a new empty output bitport.

If msf? is true, then the bytes produced by the bitport (through output-bitport-get-output) represents the sequence in most significant first bit order.

procedure

(output-bitport-partial bp)  sbv?

  bp : output-bitport?
Returns bp’s current partial bitvector, consisting of the last bits written (between 0 and 7, inclusive) that have not yet been packed into a byte.

procedure

(output-bitport-write-bit bp bit)  void?

  bp : output-bitport?
  bit : (or/c 0 1)
Writes a single bit to bp.

Equivalent to (output-bitport-write-sbv (make-sbv bit 1)).

procedure

(output-bitport-write-sbv bp sbv)  void?

  bp : output-bitport?
  sbv : sbv?
Writes the bitvector sbv to bp.

procedure

(output-bitport-get-output bp 
  [#:reset? reset? 
  #:pad pad-sbv]) 
  
bytes? exact-nonnegative-integer?
  bp : output-bitport?
  reset? : boolean? = #f
  pad-sbv : sbv? = empty-sbv
Returns (values output bits-written), where output is the accumulated output of bp as a big-endian byte string and bits-written is the number of bits written to bp. The byte string is big-endian in the following sense: the first bit written is the most significant bit of the first byte in the byte string.

If bp contains an incomplete byte (because bits-written is not divisible by 8), then the final byte of the output is padded with the lower bits of pad-sbv (extended with zeros if more padding bits are needed). If bits-written is divisible by 8, no padding is included.

If reset? is true, then all written bits are removed from bp.

procedure

(output-bitport-pad bp [#:pad pad-sbv])

  exact-nonnegative-integer?
  bp : output-bitport?
  pad-sbv : sbv? = 0
Pads the bits written to bp to a whole byte, using the lower bits of pad-sbv. If the number of bits written to bp is divisible by 8, then no padding is done. The result is the number of padding bits added (between 0 and 7, inclusive).

procedure

(bytes-bit-set? bs bit-index)  boolean?

  bs : bytes?
  bit-index : exact-nonnegative-integer?
Returns #t if the bit at bit-index is set in bs, #f otherwise.

The byte string is interpreted as big-endian in the following sense: within a single byte in bs, bits are indexed started with the most significant bit first. So for example, (bytes-bit-set? (bytes b) 0) is (bitwise-bit-set? b 7).

Examples:
> (bytes-bit-set? (bytes 1 128) 0)

#f

> (bytes-bit-set? (bytes 1 128) 7)

#t

> (bytes-bit-set? (bytes 1 128) 8)

#t

> (bytes-bit-set? (bytes 1 128) 15)

#f