For Gauche 0.9.5

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

### 11.15 srfi-60 - Integers as bits

Module: srfi-60

This srfi provides bit operations on integers, regarding them as 2’s complement representation.

Most of the functionalities in this module are already provided as builtin procedures (see Bitwise operations). This srfi defines popular aliases for many of those procedures, thus has better portability. There are also several additional procedures than Gauche’s builtin bitwise operations.

#### 11.15.1 Bitwise operators

Function: lognot n
Function: bitwise-not n

Same as builtin lognot (see Bitwise operations).

Function: logand n …
Function: bitwise-and n …

Same as builtin logand (see Bitwise operations).

Function: logior n …
Function: bitwise-ior n …

Same as builtin logior (see Bitwise operations).

Function: logxor n …
Function: bitwise-xor n …

Same as builtin logxor (see Bitwise operations).

Returns integer, whose n-th bit is taken as follows: If the n-th bit of mask is 1, the n-th bit of n0; otherwise, the n-th bit of n1.

(bitwise-if #b10101100 #b00110101 #b11001010)
⇒ #b01100110

Same as builtin logtest (see Bitwise operations).

#### 11.15.2 Integer properties

Function: logcount n
Function: bit-count n

Same as builtin logcount (see Bitwise operations).

Function: integer-length n

Same as builtin integer-length (see Bitwise operations).

Function: log2-binary-factors n
Function: first-set-bit n

Returns the number of factors of two of integer n; that is, returns a maximum k such that (expt 2 k) divides n without a remainder. It is the same as the index of the least significant 1 in n, hence the alias first-set-bit.

(log2-binary-factors 0) ⇒ -1   ; edge case
(log2-binary-factors 1) ⇒ 0
(log2-binary-factors 2) ⇒ 1
(log2-binary-factors 15) ⇒ 0
(log2-binary-factors 16) ⇒ 4

#### 11.15.3 Bit within word

Function: logbit? index n
Function: bit-set? index n

Same as builtin logbit? (see Bitwise operations).

Function: copy-bit index n bit

Same as builtin copy-bit (see Bitwise operations).

#### 11.15.4 Field of bits

Function: bit-field n start end

Same as builtin bit-field (see Bitwise operations).

Function: copy-bit-field to from start end

Same as builtin copy-bit-field (see Bitwise operations).

Function: ash n count
Function: arithmetic-shift n count

Same as builtin ash (see Bitwise operations).

Function: rotate-bit-field n count start end

Rotate the region of n between start-th bit (inclusive) and end-th bit (exclusive) by count bits.

(rotate-bit-field #b110100100010000 -1 5 9)
⇒ 26768 ; #b110100010010000

(rotate-bit-field #b110100100010000 1 5 9)
⇒ 26672 ; #b110100000110000
Function: reverse-bit-field n start end

Reverse the order of bits of n between start-th bit (inclusive) and end-th bit (exclusive).

(reverse-bit-field #b10100111 0 8)
⇒ 229 ; #b11100101

#### 11.15.5 Bit as booleans

Function: integer->list n :optional len

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 9)   ⇒ (#t #f #f #t)
(integer->list 9 6) ⇒ (#f #f #t #f #f #t)
Function: list->integer lis

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 #f #t)) ⇒ 9
Function: booleans->integer bool …

(list->integer (list bool …))

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