`(require 'logical)`

or `(require 'srfi-60)`

The bit-twiddling functions are made available through the use of the
`logical`

package. `logical`

is loaded by inserting
`(require 'logical)`

before the code that uses these functions.
These functions behave as though operating on integers in
two's-complement representation.

— Function: **logand**` n1 ...`

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

— Function:

Returns the integer which is the bit-wise AND of the integer arguments.

Example:

(number->string (logand #b1100 #b1010) 2) ⇒ "1000"

— Function: **logior**` n1 ...`

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

— Function:

Returns the integer which is the bit-wise OR of the integer arguments.

Example:

(number->string (logior #b1100 #b1010) 2) ⇒ "1110"

— Function: **logxor**` n1 ...`

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

— Function:

Returns the integer which is the bit-wise XOR of the integer arguments.

Example:

(number->string (logxor #b1100 #b1010) 2) ⇒ "110"

— Function: **lognot**` n`

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

— Function:

Returns the integer which is the one's-complement of the integer argument.

Example:

(number->string (lognot #b10000000) 2) ⇒ "-10000001" (number->string (lognot #b0) 2) ⇒ "-1"

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

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

— Function:

Returns an integer composed of some bits from integer

n0and some from integern1. A bit of the result is taken fromn0if the corresponding bit of integermaskis 1 and fromn1if that bit ofmaskis 0.

— Function: **logtest**` j k`

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

— Function:

(logtest j k) == (not (zero? (logand j k))) (logtest #b0100 #b1011) ⇒ #f (logtest #b0100 #b0111) ⇒ #t

— Function: **logcount**` n`

Returns the number of bits in integer

n. If integer is positive, the 1-bits in its binary representation are counted. If negative, the 0-bits in its two's-complement binary representation are counted. If 0, 0 is returned.Example:

(logcount #b10101010) ⇒ 4 (logcount 0) ⇒ 0 (logcount -2) ⇒ 1

On `discuss@r6rs.org`

Ben Harris credits Simon Tatham with the
idea to have `bitwise-bit-count`

return a negative count for
negative inputs. Alan Bawden came up with the succinct invariant.

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

If

nis non-negative, this procedure returns the number of 1 bits in the two's-complement representation ofn. Otherwise it returns the result of the following computation:(bitwise-not (bitwise-bit-count (bitwise-notn)))

— Function: **integer-length**` n`

Returns the number of bits neccessary to represent

n.Example:

(integer-length #b10101010) ⇒ 8 (integer-length 0) ⇒ 0 (integer-length #b1111) ⇒ 4

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

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

— Function:

Returns the number of factors of two of integer

n. This value is also the bit-index of the least-significant ‘1’ bit inn.(require 'printf) (do ((idx 0 (+ 1 idx))) ((> idx 16)) (printf "%s(%3d) ==> %-5d %s(%2d) ==> %-5d\n" 'log2-binary-factors (- idx) (log2-binary-factors (- idx)) 'log2-binary-factors idx (log2-binary-factors idx))) -| log2-binary-factors( 0) ==> -1 log2-binary-factors( 0) ==> -1 log2-binary-factors( -1) ==> 0 log2-binary-factors( 1) ==> 0 log2-binary-factors( -2) ==> 1 log2-binary-factors( 2) ==> 1 log2-binary-factors( -3) ==> 0 log2-binary-factors( 3) ==> 0 log2-binary-factors( -4) ==> 2 log2-binary-factors( 4) ==> 2 log2-binary-factors( -5) ==> 0 log2-binary-factors( 5) ==> 0 log2-binary-factors( -6) ==> 1 log2-binary-factors( 6) ==> 1 log2-binary-factors( -7) ==> 0 log2-binary-factors( 7) ==> 0 log2-binary-factors( -8) ==> 3 log2-binary-factors( 8) ==> 3 log2-binary-factors( -9) ==> 0 log2-binary-factors( 9) ==> 0 log2-binary-factors(-10) ==> 1 log2-binary-factors(10) ==> 1 log2-binary-factors(-11) ==> 0 log2-binary-factors(11) ==> 0 log2-binary-factors(-12) ==> 2 log2-binary-factors(12) ==> 2 log2-binary-factors(-13) ==> 0 log2-binary-factors(13) ==> 0 log2-binary-factors(-14) ==> 1 log2-binary-factors(14) ==> 1 log2-binary-factors(-15) ==> 0 log2-binary-factors(15) ==> 0 log2-binary-factors(-16) ==> 4 log2-binary-factors(16) ==> 4

— Function: **logbit?**` index n`

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

— Function:

(logbit? index n) == (logtest (expt 2 index) n) (logbit? 0 #b1101) ⇒ #t (logbit? 1 #b1101) ⇒ #f (logbit? 2 #b1101) ⇒ #t (logbit? 3 #b1101) ⇒ #t (logbit? 4 #b1101) ⇒ #f

— Function: **copy-bit**` index from bit`

Returns an integer the same as

fromexcept in theindexth bit, which is 1 ifbitis`#t`

and 0 ifbitis`#f`

.Example:

(number->string (copy-bit 0 0 #t) 2) ⇒ "1" (number->string (copy-bit 2 0 #t) 2) ⇒ "100" (number->string (copy-bit 2 #b1111 #f) 2) ⇒ "1011"

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

Returns the integer composed of the

start(inclusive) throughend(exclusive) bits ofn. Thestartth bit becomes the 0-th bit in the result.Example:

(number->string (bit-field #b1101101010 0 4) 2) ⇒ "1010" (number->string (bit-field #b1101101010 4 9) 2) ⇒ "10110"

— Function: **copy-bit-field**` to from start end`

Returns an integer the same as

toexcept possibly in thestart(inclusive) throughend(exclusive) bits, which are the same as those offrom. The 0-th bit offrombecomes thestartth bit of the result.Example:

(number->string (copy-bit-field #b1101101010 0 0 4) 2) ⇒ "1101100000" (number->string (copy-bit-field #b1101101010 -1 0 4) 2) ⇒ "1101101111" (number->string (copy-bit-field #b110100100010000 -1 5 9) 2) ⇒ "110100111110000"

— Function: **ash**` n count`

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

— Function:

Returns an integer equivalent to

`(inexact->exact (floor (*`

n`(expt 2`

count`))))`

.Example:

(number->string (ash #b1 3) 2) ⇒ "1000" (number->string (ash #b1010 -1) 2) ⇒ "101"

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

Returns

nwith the bit-field fromstarttoendcyclically permuted bycountbits towards high-order.Example:

(number->string (rotate-bit-field #b0100 3 0 4) 2) ⇒ "10" (number->string (rotate-bit-field #b0100 -1 0 4) 2) ⇒ "10" (number->string (rotate-bit-field #b110100100010000 -1 5 9) 2) ⇒ "110100010010000" (number->string (rotate-bit-field #b110100100010000 1 5 9) 2) ⇒ "110100000110000"

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

Returns

nwith the order of bitsstarttoendreversed.(number->string (reverse-bit-field #xa7 0 8) 16) ⇒ "e5"

— Function: **integer->list**` k len`

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

— Function:

`integer->list`

returns a list oflenbooleans corresponding to each bit of the non-negative integerk. #t is coded for each 1; #f for 0. Thelenargument defaults to`(integer-length`

k`)`

.