bitwise-bit-set?(3scm)

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

πŸ’… bitwise-bit-set?(3scm) β€” hi bestie, welcome to the Scheme Programmer's Manual ✨

okay so this page is your whole guide to bitwise-bit-set? and fxbit-set?, which live in the Scheme Programmer's Manual (3scm, if you're keeping score πŸŽ€). and yes, it's serving exactly the "is this one single bit alive" energy you'd expect. let's get into it πŸ’–


🌸 NAME 🌸

bitwise-bit-set?, fxbit-set? β€” test if a bit is set in an exact integer. that's it, that's the job, and honestly? she ate πŸ‘‘


πŸ’œ LIBRARY πŸ’œ

three different doors into the same party, pick whichever import your code already has open:

(import (rnrs))                     ;R6RS
(import (rnrs arithmetic bitwise))  ;R6RS
(import (rnrs arithmetic fixnums))  ;R6RS

πŸ¦‹ SYNOPSIS πŸ¦‹

(bitwise-bit-set? ei k)
(fxbit-set? fx k)

two procedures, two arguments each, no drama ✨


🧚 DESCRIPTION 🧚

okay so the whole vibe: these return #t if the kth bit is 1 in the two's complement representation of ei or fx respectively, and #f otherwise. that's the entire function, no cap. you hand it a number and a bit index, it tells you if that bit is turned on. bit goes brrr or it doesn't πŸ’…

and because the spec loves being extra, here's the literal result of the following computation β€” like, this is genuinely what it's computing:

; bitwise-bit-set?
(not (zero? (bitwise-and (bitwise-arithmetic-shift-left 1 k)
                         ei)))

; fxbit-set?
(if (fx>=? k (fx- (fixnum-width) 1))
    (fxnegative? fx)
    (not (fxzero? (fxand fx (fxarithmetic-shift-left 1 k)))))

so the fixnum one has that cheeky little conditional at the top for the top bit β€” iconic behavior, honestly 🫢


πŸŽ€ RETURN VALUES πŸŽ€

Returns a single boolean object. one value. a #t or a #f. that's it. lowkey the cleanest return contract ever πŸ’€


⭐ EXAMPLES ⭐

watch her work:

(bitwise-bit-set? #b101 0)
          =>  #t

(bitwise-bit-set? #b101 1)
          =>  #f

bit 0 is set in #b101, bit 1 is not. slay. look at the binary, it's right there: 101 πŸ”₯


🌈 COMPATIBILITY 🌈

This procedure is new in R6RS. also, SRFI-151 has the similar procedure bit-set?. so if you're on a different SRFI situation, you know where to look πŸ‘€ iykyk


πŸ’€ 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. Raised if k is negative. ⚠️ yeah, negative k is a no from her, like don't even try it

πŸ”₯ SEE ALSO πŸ”₯

go read the neighbors, they're all lovely:


πŸ‘‘ STANDARDS πŸ‘‘

R6RS. periodt πŸ’…


🌟 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/. genuine main character energy from the docs team fr fr 🫢


πŸ™ˆ BUGS πŸ™ˆ

Note that even in the fixnum variant, k is allowed to be greater than the fixnum width. There have been bugs related to this aspect in Sagittarius 0.4.9 and IronScheme TFS:101169. so like... she's a little wild past the fixnum width, and yes, people have been burned 😭 noted and documented, at least


okay that's the page!! manual bitwise-bit-set?(3scm), dated 2021-03-09, from the scheme-manpages project β€” go forth, check your bits, and remember: no negative k πŸŽ€βœ¨πŸ’–

πŸ“¦ where this page comes from