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.
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))
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
if it is negative, which is the exact integer that is the result of the
(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 i in an unsigned field.
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
#t if the i2’th bit (where i2 must be non-negative)
is 1 in the two’s complement representation of i1, and
otherwise. This is the result of the following computation:
(not (zero? (bitwise-and (bitwise-arithmetic-shift-left 1 i2) i1)))
Returns the result of replacing the bitno’th bit of i by replacement-bit, where bitno must be non-negative, and replacement-bit must be either 0 or 1. This is the result of the following computation:
(let* ((mask (bitwise-arithmetic-shift-left 1 bitno))) (bitwise-if mask (bitwise-arithmetic-shift-left replacement-bit bitno) i))
Returns the integer formed from the (unsigned) bit-field starting at start and ending just before end. Same as:
(let ((mask (bitwise-not (bitwise-arithmetic-shift-left -1 end)))) (bitwise-arithmetic-shift-right (bitwise-and n mask) start))
Returns the result of replacing in to the bits at positions from start (inclusive) to end (exclusive) by the bits in from from position 0 (inclusive) to position end - start (exclusive). Both start and start must be non-negative, and start must be less than or equal to 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))
Shifts i by j.
It is a “left” shift if
a “right” shift if
The result is equal to
(floor (* i (expt 2 j))).
(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
The amount must be non-negative
bitwise-arithmetic-shift-left procedure returns the same
(bitwise-arithmetic-shift-right i amount) returns the
same result as
(bitwise-arithmetic-shift i (- amount)).
If i is a primitive integer type,
then amount must be less than the number of bits in the
promoted type of i (32 or 64).
If the type is unsigned, an unsigned (logic) shift is
rather than a signed (arithmetic) shift.
Returns the result of cyclically permuting in n the bits at positions from start (inclusive) to end (exclusive) by count bits towards the more significant bits, start and end must be non-negative, and start must be less than or equal to end. This is the result of the following computation:
(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))
Returns the result obtained from i by reversing the order of the bits at positions from start (inclusive) to end (exclusive), where start and end must be non-negative, and start must be less than or equal to end.
(bitwise-reverse-bit-field #b1010010 1 4) ⇒ 88 ; #b1011000
Perform one of the 16 bitwise operations of x and y, depending on op.
Returns true if the arguments have any bits in common.
(not (zero? (bitwise-and i j))),
but is more efficient.
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
(require 'srfi-60) or
(import (srfi :60))
(import (srfi :60 integer-bits))).
(bitwise-and i ...). Builtin.
(bitwise-ior i ...). Builtin.
(bitwise-xor i ...). Builtin.
(bitwise-not i). Builtin.
(bitwise-if mask i j).
(logtest i j).
Count the number of 1-bits in i, if it is non-negative.
If i is negative, count number of 0-bits.
(bitwise-bit-count i) if i is non-negative.
(bitwise-length i). Builtin.
(bitwise-bit-set? i pos).
(bitwise-copy-bit i bitno (if bool 1 0)).
(bitwise-bit-field n start end).
(bitwise-copy-bit-field to start end from).
(bitwise-arithmetic-shift i j). Builtin.
(bitwise-rotate-bit-field n start end count).
(bitwise-reverse-bit-field i start end).
integer->list procedure returns a list of length
booleans corresponding to the bits of the non-negative integer k,
(bitwise-length k). The list will be in order
from MSB to LSB, with the value of
(odd? k) in the last
list->integer procedure returns the integer corresponding to
the booleans in the list list.
list->integer procedures are
inverses so far as
equal? is concerned.
Returns the integer coded by the bool1 ... arguments.
(list->integer (list bool1 ...)).
This older function is still available, but we recommend using the R6RS-compatible function.
(bitwise-bit-field n start end).