6.6.11 Bit Vectors

Bit vectors are zero-origin, one-dimensional arrays of booleans. They are displayed as a sequence of 0s and 1s prefixed by #*, e.g.,

(make-bitvector 8 #f) ⇒
#*00000000

Bit vectors are the special case of one dimensional bit arrays, and can thus be used with the array procedures, See Arrays.

Scheme Procedure: bitvector? obj

Return #t when obj is a bitvector, else return #f.

Scheme Procedure: make-bitvector len [fill]

Create a new bitvector of length len and optionally initialize all elements to fill.

Scheme Procedure: bitvector bit …

Create a new bitvector with the arguments as elements.

Scheme Procedure: bitvector-length vec

Return the length of the bitvector vec.

Scheme Procedure: bitvector-bit-set? vec idx
Scheme Procedure: bitvector-bit-clear? vec idx

Return #t if the bit at index idx of the bitvector vec is set (for bitvector-bit-set?) or clear (for bitvector-bit-clear?).

Scheme Procedure: bitvector-set-bit! vec idx
Scheme Procedure: bitvector-clear-bit! vec idx

Set (for bitvector-set-bit!) or clear (for bitvector-clear-bit!) the bit at index idx of the bitvector vec.

Scheme Procedure: bitvector-set-all-bits! vec
Scheme Procedure: bitvector-clear-all-bits! vec
Scheme Procedure: bitvector-flip-all-bits! vec

Set, clear, or flip all bits of vec.

Scheme Procedure: list->bitvector list
C Function: scm_list_to_bitvector (list)

Return a new bitvector initialized with the elements of list.

Scheme Procedure: bitvector->list vec
C Function: scm_bitvector_to_list (vec)

Return a new list initialized with the elements of the bitvector vec.

Scheme Procedure: bitvector-copy bitvector [start [end]]
C Function: scm_bitvector_copy (bitvector, start, end)

Returns a freshly allocated bitvector containing the elements of bitvector in the range [start ... end). start defaults to 0 and end defaults to the length of bitvector.

Scheme Procedure: bitvector-count bitvector

Return a count of how many entries in bitvector are set.

(bitvector-count #*000111000)  ⇒ 3
Scheme Procedure: bitvector-count-bits bitvector bits

Return a count of how many entries in bitvector are set, with the bitvector bits selecting the entries to consider. bitvector must be at least as long as bits.

For example,

(bitvector-count-bits #*01110111 #*11001101) ⇒ 3
Scheme Procedure: bitvector-position bitvector bool start
C Function: scm_bitvector_position (bitvector, bool, start)

Return the index of the first occurrence of bool in bitvector, starting from start. If there is no bool entry between start and the end of bitvector, then return #f. For example,

(bitvector-position #*000101 #t 0)  ⇒ 3
(bitvector-position #*0001111 #f 3) ⇒ #f
Scheme Procedure: bitvector-set-bits! bitvector bits

Set entries of bitvector to #t, with bits selecting the bits to set. The return value is unspecified. bitvector must be at least as long as bits.

(define bv (bitvector-copy #*11000010))
(bitvector-set-bits! bv #*10010001)
bv
⇒ #*11010011
Scheme Procedure: bitvector-clear-bits! bitvector bits

Set entries of bitvector to #f, with bits selecting the bits to clear. The return value is unspecified. bitvector must be at least as long as bits.

(define bv (bitvector-copy #*11000010))
(bitvector-clear-bits! bv #*10010001)
bv
⇒ #*01000010
C Function: int scm_is_bitvector (SCM obj)
C Function: SCM scm_c_make_bitvector (size_t len, SCM fill)
C Function: int scm_bitvector_bit_is_set (SCM vec, size_t idx)
C Function: int scm_bitvector_bit_is_clear (SCM vec, size_t idx)
C Function: void scm_c_bitvector_set_bit_x (SCM vec, size_t idx)
C Function: void scm_c_bitvector_clear_bit_x (SCM vec, size_t idx)
C Function: void scm_c_bitvector_set_bits_x (SCM vec, SCM bits)
C Function: void scm_c_bitvector_clear_bits_x (SCM vec, SCM bits)
C Function: void scm_c_bitvector_set_all_bits_x (SCM vec)
C Function: void scm_c_bitvector_clear_all_bits_x (SCM vec)
C Function: void scm_c_bitvector_flip_all_bits_x (SCM vec)
C Function: size_t scm_c_bitvector_length (SCM bitvector)
C Function: size_t scm_c_bitvector_count (SCM bitvector)
C Function: size_t scm_c_bitvector_count_bits (SCM bitvector, SCM bits)

C API for the corresponding Scheme bitvector interfaces.

C Function: const scm_t_uint32 * scm_bitvector_elements (SCM vec, scm_t_array_handle *handle, size_t *offp, size_t *lenp, ssize_t *incp)

Like scm_vector_elements (see Vector Accessing from C), but for bitvectors. The variable pointed to by offp is set to the value returned by scm_array_handle_bit_elements_offset. See scm_array_handle_bit_elements for how to use the returned pointer and the offset.

C Function: scm_t_uint32 * scm_bitvector_writable_elements (SCM vec, scm_t_array_handle *handle, size_t *offp, size_t *lenp, ssize_t *incp)

Like scm_bitvector_elements, but the pointer is good for reading and writing.