Next: , Previous: Condition system, Up: System facilities


4.3 Bitwise manipulation

Scheme48 provides two structures for bit manipulation: bitwise integer operations, the bitwise structure, and homogeneous vectors of bytes (integers between 0 and 255, inclusive), the byte-vectors structure.

4.3.1 Bitwise integer operations

The bitwise structure exports these procedures:

— procedure: bitwise-and integer ... –> integer
— procedure: bitwise-ior integer ... –> integer
— procedure: bitwise-xor integer ... –> integer
— procedure: bitwise-not integer –> integer

Basic twos-complement bitwise boolean logic operations.

— procedure: arithmetic-shift integer count –> integer

Shifts integer by the given bit count. If count is positive, the shift is a left shift; otherwise, it is a right shift. Arithmetic-shift preserves integer's sign.

— procedure: bit-count integer –> integer

Returns the number of bits that are set in integer. If integer is negative, it is flipped by the bitwise NOT operation before counting.

          (bit-count #b11010010)                  => 4

4.3.2 Byte vectors

The structure byte-vectors exports analogues of regular vector procedures for byte vectors, homogeneous vectors of bytes:

— procedure: make-byte-vector length fill –> byte-vector
— procedure: byte-vector byte ... –> byte-vector
— procedure: byte-vector? object –> boolean
— procedure: byte-vector-length byte-vector –> integer
— procedure: byte-vector-ref byte-vector index –> byte
— procedure: byte-vector-set! byte-vector index byte –> unspecified

Fill and each byte must be bytes, i.e. integers within the inclusive range 0 to 255. Note that make-byte-vector is not an exact analogue of make-vector, because the fill parameter is required.

Old versions of Scheme48 referred to byte vectors as `code vectors' (since they were used to denote byte code). The code-vectors structure exports make-code-vector, code-vector?, code-vector-length, code-vector-ref, and code-vector-set!, identical to the analogously named byte vector operations.