On this page:
bytes-bit-length
bytes-bit-set?
bytes-bit-set!
bytes-bits->sbv
bytes-bits->string
string->bytes-bits
8.12

8 Bytes as Bits🔗ℹ

This module provides support for interpreting a Racket byte string (bytes?) as a sequence of bits. It supports two bit indexing orders:
  • In most significant first order, the first bit in the sequence corresponds to the most significant bit of the first byte of the byte string.

    For example, the bit sequence 11000000 is represented by (bytes #b11000000).

  • In least significant first order, the first bit in the sequence corresponds to the least significant bit of the first byte of the byte string. (This is the natural order corresponding to using bitwise-bit-set? on the remainder modulo 8 of the bit index.)

    For example, the bit sequence 11000000 is represented by (bytes #b00000011).

This library uses most significant first order by default.

Added in version 1.3 of package binaryio-lib.

procedure

(bytes-bit-length bs)  exact-nonnegative-integer?

  bs : bytes?
Returns the length of bs in bits.

Equivalent to (* 8 (bytes-length bs)).

procedure

(bytes-bit-set? bs bit-index [msf?])  boolean?

  bs : bytes?
  bit-index : exact-nonnegative-integer?
  msf? : boolean? = #t
Returns #t if the bit in bs at bit-index is set (1), #f if it is unset (0).

If msf? is #t (the default), then bits within each byte are indexed in most significant first order. If msf? is #f, then bits within each byte are indexed in least significant first order.

Examples:
> (define bs (bytes #b00000001 #b00000011))
> (bytes-bit-set? bs 0)

#f

> (bytes-bit-set? bs 0 #f)

#t

> (bytes-bit-set? bs 6)

#f

> (bytes-bit-set? bs 7)

#t

procedure

(bytes-bit-set! bs bit-index value [msf?])  void?

  bs : bytes?
  bit-index : exact-nonnegative-integer?
  value : boolean?
  msf? : boolean? = #t
Sets the bit in bs at position bit-index to 1 if value is #t or 0 if value is #f.

The msf? argument determines how bit-index is interpreted as for bytes-bit-set?.

procedure

(bytes-bits->sbv bs [start-biti end-biti msf?])  sbv?

  bs : bytes?
  start-biti : exact-nonnegative-integer? = 0
  end-biti : (or/c #f exact-nonnegative-integer?) = #f
  msf? : boolean? = #t
Returns a short bitvector representing the subsequence of bits in bs from start-biti (inclusive) to end-biti (exclusive). If end-biti is #f, it is interpreted as (bytes-bit-length bs).

Examples:
> (sbv->string (bytes-bits->sbv bs 4 12))

"00010000"

> (sbv->string (bytes-bits->sbv bs 12 16))

"0011"

> (bytes-bits->string bs 12 16 #t)

"0011"

> (sbv->string (bytes-bits->sbv bs 12 16 #f))

"0000"

> (bytes-bits->string bs 12 16 #f)

"0000"

procedure

(bytes-bits->string bs    
  [start-biti    
  end-biti    
  msf?])  string?
  bs : bytes?
  start-biti : exact-nonnegative-integer? = 0
  end-biti : (or/c #f exact-nonnegative-integer?) = #f
  msf? : boolean? = #t
Returns a string of 0 and 1 characters representing the bits of bs from start-biti (inclusive) to end-biti (exclusive). If end-biti is #f, it is interpreted as (bytes-bit-length bs).

Examples:
> (bytes-bits->string bs)

"0000000100000011"

> (bytes-bits->string bs 0 #f #f)

"1000000011000000"

procedure

(string->bytes-bits s [msf?])  bytes?

  s : (and/c string? #rx"^[01]$")
  msf? : boolean? = #t
Parses s, which should consist of 0 and 1 characters, and returns the byte string representing the bit sequence. If the length of s is not a multiple of 8, then the sequence is padded with 0 bits.

Example:
> (string->bytes-bits "00010011")

#"\23"