FXFIRST-BIT-SET(3scm)

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

πŸ’… bitwise-first-bit-set / fxfirst-bit-set β€” the Scheme Programmer's Manual, but make it cute

okay so bestie, this is the page for bitwise-first-bit-set and fxfirst-bit-set from the Scheme Programmer's Manual (that's the 3scm manual, iykyk). It's literally all about finding the first bit that's set in an integer. Tiny concept, iconic energy. ✨


🌸 NAME

bitwise-first-bit-set, fxfirst-bit-set β€” first bit set in an integer


πŸŽ€ LIBRARY

these are the imports you need, depending on which flavour you're living in:

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

πŸ¦‹ SYNOPSIS

(bitwise-first-bit-set ei)
(fxfirst-bit-set fx)

πŸ’– DESCRIPTION

So what it does is: it returns the index of the least significant 1 bit in the two's complement representation of the argument. If the argument is 0, then βˆ’1 is returned. That's the whole vibe check. πŸ’…

The argument to fxfirst-bit-set must be in the implementation's fixnum range β€” no cap, that's the rule. 🫢


🌈 RETURN VALUES

Returns a single value; an exact integer. One value, one vibe, no drama. ✨


πŸ“ EXAMPLES

(bitwise-first-bit-set 0)   =>  -1
(bitwise-first-bit-set 1)   =>  0
(bitwise-first-bit-set -4)  =>  2

look at that. ate. πŸ‘‘


πŸ’œ APPLICATION USAGE

These procedures have a variety of uses, e.g. for using integers to represent sets, and some bit-hacks. sets made of integers? bit-hacks? obsessed. 🧚


⭐ RATIONALE

Some CPUs have an instruction that is similar to this procedure, such as the BSF instruction in the x86 ISA. If it was instead defined by user code then it would be much slower. so yeah, hardware said "i got this" and honestly? it did. πŸ”₯


πŸ¦‹ COMPATIBILITY

The fixnum variant of this procedure works with the implementation-defined fixnum range. that's it, that's the note. πŸ’€


😭 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.
bestie if you get this one, check your arg count first. fr fr. πŸ™ˆ

πŸ’… SEE ALSO

bitwise-length(3scm)


🌟 STANDARDS

R4RS, IEEE Scheme, R5RS, R6RS, R7RS, SRFI-60, SRFI-143, SRFI-151


πŸŽ€ HISTORY

These procedures are new in R6RS. fresh out the box, 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/.


and that's the whole page, bestie 🫢 β€” source/version situation: this is bitwise-...t-bit-set(3scm), dated 2022-04-09. go find that first set bit and slay πŸ’…βœ¨

πŸ“¦ where this page comes from