bitwise-copy-bit(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

✨ 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: k must be non-negative, and bit must be either 0 or 1. and the fixnum variant is even pickier β€” it further requires that n is a fixnum, and that k is non-negative and less than (fixnum-width)-1. so yeah, it's giving strict 😭

πŸ” SEE ALSO πŸ”

πŸ‘‘ 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