bitwise-copy-bit(3scm)
β¨ bitwise-copy-bit β the 3scm girlies' guide β¨
okay so bestie, we're deep in the Scheme Programmer's Manual today and this one is bitwise-copy-bit(3scm) β the little page about copying one single bit from one integer onto another. lowkey iconic behaviour honestly π
πΈ NAME πΈ
bitwise-copy-bit, fxcopy-bit β copying a bit from an integer onto another integer β¨
π LIBRARY π
like, import it first bestie, obviously:
(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 π
so basically it returns the result of replacing the kth bit of n by bit. that's it. that's the whole vibe. one bit gets swapped out, everything else stays exactly where it was. no cap π«Ά
π§ IMPLEMENTATION NOTES π§
the R6RS girlies blessed us with these reference implementations, and honestly? ate. π₯
;; 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, and it's an exact integer. one value. single. solo. main character energy π
β EXAMPLES β
look at these little cuties go:
(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, no cap. but like, similar procedures do exist out there in SRFI-60 and SRFI-151 if you're feeling adventurous π¦
π ERRORS π
this procedure can raise exceptions with the following condition types, so pay attention bestie:
&assertion(R6RS) β the wrong number of arguments was passed, or an argument was outside its domain. specifically:kmust be non-negative, andbitmust be either0or1. and the fixnum variant is even pickier β it further requires thatnis a fixnum, and thatkis non-negative and less than(fixnum-width)-1. so yeah, it's giving strict π
π SEE ALSO π
bitwise-if(3scm)bitwise-bit-set?(3scm)
π STANDARDS π
R6RS. periodt.
πΈ HISTORY πΈ
these procedures first appeared in R6RS. there was previously a similar procedure in SRFI-60 called copy-bit. so like, the concept has been around, she just got a glow-up β¨
π«Ά 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/. shoutout to the maintainers, they're doing the lord's work honestly π
π BUGS π
if k is very large then an implementation might try to allocate an excessive amount of memory. like, it might just go feral and eat all your RAM π in normal implementations it is expected that all practical values of k are fixnums. so maybe don't come for her with a k of like a billion, iykyk π
and that's the page bestie! from the Scheme Programmer's Manual, dated 2023-02-12 β bitwise-copy-bit(3scm), signed, sealed, delivered π
π go forth and copy some bits β¨
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2023 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms