Next: , Previous: Uniform Numeric Vectors, Up: Compound Data Types


5.6.5 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 are also generalized vectors, See Generalized Vectors, and can thus be used with the array procedures, See Arrays. Bit vectors are the special case of one dimensional bit arrays.

— Scheme Procedure: bitvector? obj
— C Function: scm_bitvector_p (obj)

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

— C Function: int scm_is_bitvector (SCM obj)

Return 1 when obj is a bitvector, else return 0.

— Scheme Procedure: make-bitvector len [fill]
— C Function: scm_make_bitvector (len, fill)

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

— C Function: SCM scm_c_make_bitvector (size_t len, SCM fill)

Like scm_make_bitvector, but the length is given as a size_t.

— Scheme Procedure: bitvector . bits
— C Function: scm_bitvector (bits)

Create a new bitvector with the arguments as elements.

— Scheme Procedure: bitvector-length vec
— C Function: scm_bitvector_length (vec)

Return the length of the bitvector vec.

— C Function: size_t scm_c_bitvector_length (SCM vec)

Like scm_bitvector_length, but the length is returned as a size_t.

— Scheme Procedure: bitvector-ref vec idx
— C Function: scm_bitvector_ref (vec, idx)

Return the element at index idx of the bitvector vec.

— C Function: SCM scm_c_bitvector_ref (SCM obj, size_t idx)

Return the element at index idx of the bitvector vec.

— Scheme Procedure: bitvector-set! vec idx val
— C Function: scm_bitvector_set_x (vec, idx, val)

Set the element at index idx of the bitvector vec when val is true, else clear it.

— C Function: SCM scm_c_bitvector_set_x (SCM obj, size_t idx, SCM val)

Set the element at index idx of the bitvector vec when val is true, else clear it.

— Scheme Procedure: bitvector-fill! vec val
— C Function: scm_bitvector_fill_x (vec, val)

Set all elements of the bitvector vec when val is true, else clear them.

— 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: bit-count bool bitvector
— C Function: scm_bit_count (bool, bitvector)

Return a count of how many entries in bitvector are equal to bool. For example,

          (bit-count #f #*000111000)  => 6
     
— Scheme Procedure: bit-position bool bitvector start
— C Function: scm_bit_position (bool, bitvector, start)

Return the index of the first occurrance 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,

          (bit-position #t #*000101 0)  => 3
          (bit-position #f #*0001111 3) => #f
     
— Scheme Procedure: bit-invert! bitvector
— C Function: scm_bit_invert_x (bitvector)

Modify bitvector by replacing each element with its negation.

— Scheme Procedure: bit-set*! bitvector uvec bool
— C Function: scm_bit_set_star_x (bitvector, uvec, bool)

Set entries of bitvector to bool, with uvec selecting the entries to change. The return value is unspecified.

If uvec is a bit vector, then those entries where it has #t are the ones in bitvector which are set to bool. uvec and bitvector must be the same length. When bool is #t it's like uvec is OR'ed into bitvector. Or when bool is #f it can be seen as an ANDNOT.

          (define bv #*01000010)
          (bit-set*! bv #*10010001 #t)
          bv
          => #*11010011
     

If uvec is a uniform vector of unsigned long integers, then they're indexes into bitvector which are set to bool.

          (define bv #*01000010)
          (bit-set*! bv #u(5 2 7) #t)
          bv
          => #*01100111
     
— Scheme Procedure: bit-count* bitvector uvec bool
— C Function: scm_bit_count_star (bitvector, uvec, bool)

Return a count of how many entries in bitvector are equal to bool, with uvec selecting the entries to consider.

uvec is interpreted in the same way as for bit-set*! above. Namely, if uvec is a bit vector then entries which have #t there are considered in bitvector. Or if uvec is a uniform vector of unsigned long integers then it's the indexes in bitvector to consider.

For example,

          (bit-count* #*01110111 #*11001101 #t) => 3
          (bit-count* #*01110111 #u(7 0 4) #f)  => 2
     
— 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.