This SRFI provides various functions for treating integers as bits and for bitwise manipulations. These functions can be obtained with,

(use-modules (srfi srfi-60))

Integers are treated as infinite precision twos-complement, the same as in the core logical functions (see Bitwise Operations). And likewise bit indexes start from 0 for the least significant bit. The following functions in this SRFI are already in the Guile core,

`logand`

,`logior`

,`logxor`

,`lognot`

,`logtest`

,`logcount`

,`integer-length`

,`logbit?`

,`ash`

— Function: **bitwise-and**` n1 ...`

— Function:**bitwise-ior**` n1 ...`

— Function:**bitwise-xor**` n1 ...`

— Function:**bitwise-not**` n`

— Function:**any-bits-set?**` j k`

— Function:**bit-set?**` index n`

— Function:**arithmetic-shift**` n count`

— Function:**bit-field**` n start end`

— Function:**bit-count**` n`

— Function:

— Function:

— Function:

— Function:

— Function:

— Function:

— Function:

— Function:

Aliases for

`logand`

,`logior`

,`logxor`

,`lognot`

,`logtest`

,`logbit?`

,`ash`

,`bit-extract`

and`logcount`

respectively.Note that the name

`bit-count`

conflicts with`bit-count`

in the core (see Bit Vectors).

— Function: **bitwise-if**` mask n1 n0`

— Function:**bitwise-merge**` mask n1 n0`

— Function:

Return an integer with bits selected from

n1andn0according tomask. Those bits wheremaskhas 1s are taken fromn1, and those wheremaskhas 0s are taken fromn0.(bitwise-if 3 #b0101 #b1010) ⇒ 9

— Function: **log2-binary-factors**` n`

— Function:**first-set-bit**` n`

— Function:

Return a count of how many factors of 2 are present in

n. This is also the bit index of the lowest 1 bit inn. Ifnis 0, the return is -1.(log2-binary-factors 6) ⇒ 1 (log2-binary-factors -8) ⇒ 3

— Function: **copy-bit**` index n newbit`

Return

nwith the bit atindexset according tonewbit.newbitshould be`#t`

to set the bit to 1, or`#f`

to set it to 0. Bits other than atindexare unchanged in the return.(copy-bit 1 #b0101 #t) ⇒ 7

— Function: **copy-bit-field**` n newbits start end`

Return

nwith the bits fromstart(inclusive) toend(exclusive) changed to the valuenewbits.The least significant bit in

newbitsgoes tostart, the next tostart+1, etc. Anything innewbitspast theendgiven is ignored.(copy-bit-field #b10000 #b11 1 3) ⇒ #b10110

— Function: **rotate-bit-field**` n count start end`

Return

nwith the bit field fromstart(inclusive) toend(exclusive) rotated upwards bycountbits.

countcan be positive or negative, and it can be more than the field width (it'll be reduced modulo the width).(rotate-bit-field #b0110 2 1 4) ⇒ #b1010

— Function: **reverse-bit-field**` n start end`

Return

nwith the bits fromstart(inclusive) toend(exclusive) reversed.(reverse-bit-field #b101001 2 4) ⇒ #b100101

— Function: **integer->list**` n `[`len`]

Return bits from

nin the form of a list of`#t`

for 1 and`#f`

for 0. The least significantlenbits are returned, and the first list element is the most significant of those bits. Iflenis not given, the default is`(integer-length`

n`)`

(see Bitwise Operations).(integer->list 6) ⇒ (#t #t #f) (integer->list 1 4) ⇒ (#f #f #f #t)

— Function: **list->integer**` lst`

— Function:**booleans->integer**` bool...`

— Function:

Return an integer formed bitwise from the given

lstlist of booleans, or for`booleans->integer`

from theboolarguments.Each boolean is

`#t`

for a 1 and`#f`

for a 0. The first element becomes the most significant bit in the return.(list->integer '(#t #f #t #f)) ⇒ 10