For Gauche 0.9.6


Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]

11.14 srfi-60 - Integers as bits

Module: srfi-60

This srfi provides bit operations on integers, regarding them as 2’s complement representation. It is compatible to SLIB’s logical module.

The newer srfi-151 (see Bitwise operations) provides the same functionality and more, with more consistent naming. We recommend new code to use srfi-151, while we keep srfi-60 for the backward compatibility.

The following procedures are Gauche built-in. See Basic bitwise operations, for the description.

lognot           logand           logior           logxor
logtest          logcount         integer-length   logbit?
copy-bit         bit-field        copy-bit-field   ash

The following procedures are defined in srfi-151. See Bitwise operations, for the description.

bitwise-not      bitwise-and      bitwise-ior      bitwise-xor
arithmetic-shift bit-count        bitwise-if       bit-set?
copy-bit         first-set-bit    

We describe procedures that are unique in srfi-60 below.

Function: bitwise-merge mask n0 n1

[SRFI-60] {srfi-60} Same as bitwise-if (see Bitwise operations).

Function: any-bits-set? mask n

[SRFI-60] {srfi-60} Same as builtin logtest (see Basic bitwise operations). It is also called any-bit-set? in srfi-151 (see Bitwise operations).

Function: log2-binary-factors n

[SRFI-60] {srfi-60} It is also called as first-set-bit in this srfi, which is also in srfi-151 (see Bitwise operations). This is equivalent to Gauche’s built-in twos-exponent-factor (see Basic bitwise operations).

Function: rotate-bit-field n count start end

[SRFI-60] {srfi-60} This is equivalent to bit-field-rotate in srfi-151 (see Bitwise operations).

Function: reverse-bit-field n start end

[SRFI-60] {srfi-60} This is equivalent to bit-field-reverse in srfi-151 (see Bitwise operations).

Function: integer->list n :optional len

[SRFI-60] {srfi-60} Breaks n to each bits, representing 1 as #t and 0 as #f, LSB last, and returns a list of them. If a nonnegative integer len is given, it specifies the length of the result. If it is omitted, (integer-length n) is used.

(integer->list 10)   ⇒ (#t #f #t #f)
(integer->list 10 6) ⇒ (#f #f #t #f #t #f)

Srfi-151 has similar procedure bits->list, with a reversed bit order (LSB first) (see Bitwise operations).

Function: list->integer lis

[SRFI-60] {srfi-60} Takes a list of boolean values, replaces the true value for 1 and the false value for 0, and compose an integer regarding each value as a binary digit. If n is nonnegative integer, (eqv? (list->integer (integer->list n)) n) is true.

(list->integer '(#f #t #f #t #f)) ⇒ 10

Srfi-151 has similar procedure list->bits, with a reversed bit order (LSB first) (see Bitwise operations).

Function: booleans->integer bool …

[SRFI-60] {srfi-60} ≡ (list->integer (list bool …))

Srfi-151 has similar procedure bits, with a reversed bit order (LSB first) (see Bitwise operations).


Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]