bitwise-copy-bit, fxcopy-bit - copy a bit from an integer onto another integer
(import (rnrs)) ;R6RS
(import (rnrs arithmetic bitwise)) ;R6RS
(import (rnrs arithmetic fixnums)) ;R6RS
(bitwise-copy-bit n k bit)
(fxcopy-bit n k bit)
Returns the result of replacing the k th bit of n by bit.
The following are reference implementations from R6RS.
;; bitwise-copy-bit
(let ((mask (bitwise-arithmetic-shift-left 1 k)))
(bitwise-if mask
(bitwise-arithmetic-shift-left bit k)
n))
;; fxcopy-bit
(let ((mask (fxarithmetic-shift-left 1 k)))
(fxif mask
(fxarithmetic-shift-left bit k)
n))
Returns a single value; an exact integer.
(bitwise-copy-bit 0 10 1) => 1024
(bitwise-copy-bit #x8000 0 1) => #x8001
(bitwise-copy-bit #x8001 15 0) => #x0001
These procedures are unique to R6RS. Similar procedures can be found in SRFI-60
and SRFI-151.
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
- The wrong number of arguments was passed or an argument was outside its
domain. In particular, k must be non-negative, and bit must
be either 0 or 1. The fixnum variant further requires that n is a
fixnum and that k is non-negative and less than
(fixnum-width)-1.
These procedures first appeared in R6RS. There was previously a similar
procedure in SRFI-60, copy-bit.
If k is very large then an implementation might try to allocate an
excessive amount of memory. In normal implementations it is expected that all
practical values of k are fixnums.