bitwise-if(3scm)

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

✨ 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