bitwise-copy-bit, fxcopy-bit - copy a bit from an integer onto another integer
LIBRARY
(import (rnrs)) ;R6RS
(import (rnrs arithmetic bitwise)) ;R6RS
(import (rnrs arithmetic fixnums)) ;R6RS
SYNOPSIS
(bitwise-copy-bit n k bit)
(fxcopy-bit n k bit)
DESCRIPTION
Returns the result of replacing the
k th
bit of
n
by
bit.
IMPLEMENTATION NOTES
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))
RETURN VALUES
Returns a single value; an exact integer.
EXAMPLES
(bitwise-copy-bit 0 10 1) => 1024
(bitwise-copy-bit #x8000 0 1) => #x8001
(bitwise-copy-bit #x8001 15 0) => #x0001
COMPATIBILITY
These procedures are unique to R6RS. Similar procedures can be found
in SRFI-60 and SRFI-151.
ERRORS
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.
SEE ALSO
bitwise-if(3scm),
bitwise-bit-set?(3scm)
STANDARDS
R6RS
HISTORY
These procedures first appeared in R6RS. There was previously a
similar procedure in SRFI-60,
copy-bit.
AUTHORS
This page is part of the
scheme-manpages
project.
It includes materials from the RnRS documents.
More information can be found at
https://github.com/schemedoc/manpages/
.
BUGS
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.
Markup created by unroff 1.0sc, March 04, 2023.