Next: SRFI-61, Previous: SRFI-55, Up: SRFI Support
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
Aliases for
logand
,logior
,logxor
,lognot
,logtest
,logbit?
,ash
,bit-extract
andlogcount
respectively.Note that the name
bit-count
conflicts withbit-count
in the core (see Bit Vectors).
Return an integer with bits selected from n1 and n0 according to mask. Those bits where mask has 1s are taken from n1, and those where mask has 0s are taken from n0.
(bitwise-if 3 #b0101 #b1010) => 9
Return a count of how many factors of 2 are present in n. This is also the bit index of the lowest 1 bit in n. If n is 0, the return is -1.
(log2-binary-factors 6) => 1 (log2-binary-factors -8) => 3
Return n with the bit at index set according to newbit. newbit should be
#t
to set the bit to 1, or#f
to set it to 0. Bits other than at index are unchanged in the return.(copy-bit 1 #b0101 #t) => 7
Return n with the bits from start (inclusive) to end (exclusive) changed to the value newbits.
The least significant bit in newbits goes to start, the next to start+1, etc. Anything in newbits past the end given is ignored.
(copy-bit-field #b10000 #b11 1 3) => #b10110
Return n with the bit field from start (inclusive) to end (exclusive) rotated upwards by count bits.
count can 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
Return n with the bits from start (inclusive) to end (exclusive) reversed.
(reverse-bit-field #b101001 2 4) => #b100101
Return bits from n in the form of a list of
#t
for 1 and#f
for 0. The least significant len bits are returned, and the first list element is the most significant of those bits. If len is 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)
Return an integer formed bitwise from the given lst list of booleans, or for
booleans->integer
from the bool arguments.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