bitwise-if(3scm)
β¨ bitwise-if(3scm) β a page from the Scheme Programmer's Manual β¨
okay bestie, pull up a chair because today we're learning about the bitwise "if" π this page lives in the Scheme Programmer's Manual, and it's genuinely so much fun once it clicks, i promise π«Ά
πΈ NAME πΈ
bitwise-if, fxif β bitwise if. that's it. that's the name. iconic, concise, no notes π
π LIBRARY π
you import these like this, exact vibes, don't freestyle:
(import (rnrs)) ;R6RS
(import (rnrs arithmetic bitwise)) ;R6RS
(import (rnrs arithmetic fixnums)) ;R6RS
π SYNOPSIS π
(bitwise-if mask a b)
(fxif mask a b)
π¦ DESCRIPTION π¦
okay so this returns the number that is the bit-wise "if" of the two's complement representations of its arguments π§ literally what does that mean though β bestie it means this: for each bit, if that bit is 1 in mask, then the corresponding bit in a becomes the value of the corresponding bit in the result β¨ and if that mask bit is 0, then the corresponding bit in b becomes the corresponding bit in the value of the result π
so mask is the main character deciding which of the other two gets to shine, and honestly? main character energy fr fr π
π₯ IMPLEMENTATION NOTES π₯
the following are reference implementations from R6RS β copy them exactly, they're canon:
; bitwise-if
(bitwise-ior (bitwise-and mask a)
(bitwise-and (bitwise-not mask) b))
; fxif
(fxior (fxand mask a)
(fxand (fxnot mask) b))
β RETURN VALUES β
returns a single value; an exact integer π and the fixnum variant returns a fixnum, obviously, it said fixnum in the name, we been knew π
π EXAMPLES π
look at these little cuties actually working:
(bitwise-if #xf0f0 #x1234 #xABCD) => #x1B3D
(bitwise-if #b00111100 #b11110000 #b00001111) => #b00110011
ate and left no crumbs, no cap π½οΈ
π COMPATIBILITY π
these procedures are unique to R6RS πΈ compatible procedures can be found in SRFI-60 and SRFI-151, so if you're on a different scheme situation, go say hi to those iykyk
π ERRORS π
these procedures can raise exceptions with the following condition types β warning time, bestie, read it carefully π
&assertion(R6RS) β the wrong number of arguments was passed or an argument was outside its domain. basically you handed it something it can't work with and it's gonna be dramatic about it π- R7RS β the assertions described above are errors. implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically π like, catastrophically, that's the actual word in the spec, so maybe don't tempt fate fr fr
ngl the "fail catastrophically" part is not a vibe but it IS in the manual so i'm telling you. be normal with your arguments and you'll be fine π«Ά
π SEE ALSO π
go read these next, they're the friend group:
π― STANDARDS π―
R6RS β that's the standard, that's the whole standard, thank you for coming to my ted talk β¨
π°οΈ HISTORY π°οΈ
these procedures first appeared in R6RS which got the idea from SRFI-60 β so like, SRFI-60 walked so R6RS could run, we love an origin story π±
π 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/. absolute legends, all of them π
and that's the whole page bestie, you're officially a bitwise-if girlie now π§π
this was bitwise-if(3scm) from the Scheme Programmer's Manual, dated 2023-02-12 β go forth and mask responsibly, ily, bye π¦π
π¦ 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