These functions operate on the 2’s complement binary representation of an exact integer.

Returns the bit-wise logical inverse of the argument. More formally, returns the exact integer whose two’s complement representation is the one’s complement of the two’s complement representation of

.`i`

These procedures return the exact integer that is the bit-wise “and”, “inclusive or”, or “exclusive or” of the two’s complement representations of their arguments. If they are passed only one argument, they return that argument. If they are passed no arguments, they return the integer that acts as identity for the operation: -1, 0, or 0, respectively.

Procedure: `bitwise-if`

`i1`

`i2`

`i3`

Returns the exact integer that is the bit-wise “if” of the twos complement representations of its arguments, i.e. for each bit, if it is 1 in i1, the corresponding bit in i2 becomes the value of the corresponding bit in the result, and if it is 0, the corresponding bit in i3 becomes the corresponding bit in the value of the result. This is equivaent to the following computation:

(bitwise-ior (bitwise-and i1 i2) (bitwise-and (bitwise-not i1) i3))

Procedure: `bitwise-bit-count`

`i`

If i is non-negative, returns the number of 1 bits in the twos complement representation of i. Otherwise it returns the result of the following computation:

(bitwise-not (bitwise-bit-count (bitwise-not i)))

Returns the number of bits needed to represent i if it is positive, and the number of bits needed to represent

`(bitwise-not`

if it is negative, which is the exact integer that is the result of the following computation:)`i`

(do ((result 0 (+ result 1)) (bits (if (negative? i) (bitwise-not i) ei) (bitwise-arithmetic-shift bits -1))) ((zero? bits) result))This is the number of bits needed to represent

in an unsigned field.`i`

Procedure: `bitwise-first-bit-set`

`i`

Returns the index of the least significant 1 bit in the twos complement representation of i. If i is 0, then - 1 is returned.

(bitwise-first-bit-set 0) ⇒ -1 (bitwise-first-bit-set 1) ⇒ 0 (bitwise-first-bit-set -4) ⇒ 2

Procedure: `bitwise-bit-set?`

`i1`

`i2`

Returns

`#t`

if the i2’th bit (wheremust be non-negative) is 1 in the two’s complement representation of`i2`

, and`i1`

`#f`

otherwise. This is the result of the following computation:(not (zero? (bitwise-and (bitwise-arithmetic-shift-left 1 i2) i1)))

Procedure: `bitwise-copy-bit`

`i`

`bitno`

`replacement-bit`

Returns the result of replacing the

’th bit of`bitno`

by`i`

, where`replacement-bit`

must be non-negative, and`bitno`

must be either 0 or 1. This is the result of the following computation:`replacement-bit`

(let* ((mask (bitwise-arithmetic-shift-left 1 bitno))) (bitwise-if mask (bitwise-arithmetic-shift-left replacement-bit bitno) i))

Procedure: `bitwise-bit-field`

`n`

`start`

`end`

Returns the integer formed from the (unsigned) bit-field starting at

and ending just before`start`

. Same as:`end`

(let ((mask (bitwise-not (bitwise-arithmetic-shift-left -1)))) (bitwise-arithmetic-shift-right (bitwise-and`end`

mask)`n`

))`start`

Procedure: `bitwise-copy-bit-field`

`to`

`start`

`end`

`from`

Returns the result of replacing in

the bits at positions from`to`

(inclusive) to`start`

(exclusive) by the bits in`end`

from position 0 (inclusive) to position`from`

-`end`

(exclusive). Both`start`

and`start`

must be non-negative, and`start`

must be less than or equal to`start`

.`start`

This is the result of the following computation:

(let* ((mask1 (bitwise-arithmetic-shift-left -1 start)) (mask2 (bitwise-not (bitwise-arithmetic-shift-left -1 end))) (mask (bitwise-and mask1 mask2))) (bitwise-if mask (bitwise-arithmetic-shift-left from start) to))

Procedure: `bitwise-arithmetic-shift`

`i`

`j`

Shifts

by`i`

. It is a “left” shift if`j`

`, and a “right” shift if`

>0`j`

`. The result is equal to`

<0`j`

`(floor (*`

.(expt 2`i`

)))`j`

Examples:

(bitwise-arithmetic-shift -6 -1) ⇒-3 (bitwise-arithmetic-shift -5 -1) ⇒ -3 (bitwise-arithmetic-shift -4 -1) ⇒ -2 (bitwise-arithmetic-shift -3 -1) ⇒ -2 (bitwise-arithmetic-shift -2 -1) ⇒ -1 (bitwise-arithmetic-shift -1 -1) ⇒ -1

Procedure: `bitwise-arithmetic-shift-left`

`i`

`amount`

Procedure: `bitwise-arithmetic-shift-right`

`i`

`amount`

The

must be non-negative The`amount`

`bitwise-arithmetic-shift-left`

procedure returns the same result as`bitwise-arithmetic-shift`

, and`(bitwise-arithmetic-shift-right`

returns the same result as`i`

)`amount`

`(bitwise-arithmetic-shift`

.(-`i`

))`amount`

If

is a primitive integer type, then`i`

must be less than the number of bits in the promoted type of`amount`

(32 or 64). If the type is unsigned, an unsigned (logic) shift is done for`i`

`bitwise-arithmetic-shift-right`

, rather than a signed (arithmetic) shift.

Procedure: `bitwise-rotate-bit-field`

`n`

`start`

`end`

`count`

Returns the result of cyclically permuting in

the bits at positions from`n`

(inclusive) to`start`

(exclusive) by`end`

bits towards the more significant bits,`count`

and`start`

must be non-negative, and`end`

must be less than or equal to`start`

. This is the result of the following computation:`end`

(let* ((n ei1) (width (- end start))) (if (positive? width) (let* ((count (mod count width)) (field0 (bitwise-bit-field n start end)) (field1 (bitwise-arithmetic-shift-left field0 count)) (field2 (bitwise-arithmetic-shift-right field0 (- width count))) (field (bitwise-ior field1 field2))) (bitwise-copy-bit-field n start end field)) n))

Procedure: `bitwise-reverse-bit-field`

`i`

`start`

`end`

Returns the result obtained from

by reversing the order of the bits at positions from`i`

(inclusive) to`start`

(exclusive), where`end`

and`start`

must be non-negative, and`end`

must be less than or equal to`start`

.`end`

(bitwise-reverse-bit-field #b1010010 1 4) ⇒ 88 ; #b1011000

Perform one of the 16 bitwise operations of

and`x`

, depending on`y`

.`op`

Returns true if the arguments have any bits in common. Same as

`(not (zero? (bitwise-and`

, but is more efficient.`i`

)))`j`

Kawa supports SRFI-60 “Integers as Bits” as well, although we
generally recommend using the R6RS-compatible functions instead when
possible. Unless noted as being a builtin function, to use these you
must first `(require 'srfi-60)`

or `(import (srfi :60))`

(or `(import (srfi :60 integer-bits))`

).

Equivalent to

`(bitwise-and`

. Builtin....)`i`

Equivalent to

`(bitwise-ior`

. Builtin....)`i`

Equivalent to

`(bitwise-xor`

. Builtin....)`i`

Equivalent to

`(bitwise-not`

. Builtin.)`i`

Procedure: `bitwise-merge`

`mask`

`i`

`j`

Equivalent to

`(bitwise-if`

.`mask`

`i`

)`j`

Equivalent to

`(logtest`

.`i`

)`j`

Count the number of 1-bits in

, if it is non-negative. If`i`

is negative, count number of 0-bits. Same as`i`

`(bitwise-bit-count`

if)`i`

is non-negative. Builtin as`i`

`logcount`

.

Equivalent to

`(bitwise-length`

. Builtin.)`i`

Procedure: `log2-binary-factors`

`i`

Equivalent to

`(bitwise-first-bit-set`

.)`i`

Equivalent to

`(bitwise-bit-set?`

.`i`

)`pos`

Procedure: `copy-bit`

`bitno`

`i`

`bool`

Equivalent to

`(bitwise-copy-bit`

.`i`

(if`bitno`

1 0))`bool`

Procedure: `bit-field`

`n`

`start`

`end`

Equivalent to

`(bitwise-bit-field`

.`n`

`start`

)`end`

Procedure: `copy-bit-field`

`to`

`from`

`start`

`end`

Equivalent to

`(bitwise-copy-bit-field`

.`to`

`start`

`end`

)`from`

Procedure: `arithmetic-shift`

`i`

`j`

Equivalent to

`(bitwise-arithmetic-shift`

. Builtin.`i`

)`j`

Alias for

`arithmetic-shift`

. Builtin.

Procedure: `rotate-bit-field`

`n`

`count`

`start`

`end`

Equivalent to

`(bitwise-rotate-bit-field`

.`n`

`start`

`end`

)`count`

Procedure: `reverse-bit-field`

`i`

`start`

`end`

Equivalent to

`(bitwise-reverse-bit-field`

.`i`

`start`

)`end`

Procedure: `integer->list`

[

`length`

The

`integer->list`

procedure returns a list ofbooleans corresponding to the bits of the non-negative integer`length`

, with`k`

`#t`

for`1`

and`#f`

for`0`

.defaults to`length`

`(bitwise-length`

. The list will be in order from MSB to LSB, with the value of)`k`

`(odd?`

in the last car.)`k`

The

`list->integer`

procedure returns the integer corresponding to the booleans in the list. The`list`

`integer->list`

and`list->integer`

procedures are inverses so far as`equal?`

is concerned.

Procedure: `booleans->integer`

`bool1`

`...`

Returns the integer coded by the

... arguments. Equivalent to`bool1`

`(list->integer (list`

....))`bool1`