BITWISE-REVERSE-BIT-FIELD(3scm)

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

πŸ’… bitwise-reverse-bit-field, fxreverse-bit-field β€” NAME

okay so bestie, welcome to the Scheme Programmer's Manual page for bitwise-reverse-bit-field and fxreverse-bit-field β€” the two procedures that literally reverse bit order in an integer ✨


πŸ“š LIBRARY

like, here's where you import them from, depending on your vibe:

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

✨ SYNOPSIS

(bitwise-reverse-bit-field n start end)
(fxreverse-bit-field n start end)

πŸ¦‹ DESCRIPTION

So what these do is: they return the integer you get by reversing the order of the bits in n, but only at the positions from start (inclusive) all the way up to end (exclusive). the way that little window of bits just gets, like, flipped around πŸ’…


πŸŽ€ RETURN VALUES

You get exactly one value back β€” an integer. that's it, that's the whole vibe. 🫢


🌸 EXAMPLES

okay let's actually look at this because the visual is iconic:

(bitwise-reverse-bit-field #b0011 0 4)
                                  ;   ||
                                  ;   vv
                                  => #b1100

see how #b0011 just became #b1100? the bits did a little spin πŸŒ€

(bitwise-reverse-bit-field #xAA 0 8)
        => #x55

and for a wider field:

(bitwise-reverse-bit-field #xAABBCCDD 24 32)
        => #x55BBCCDD

only the top byte moved, the rest was unbothered. main character energy honestly. πŸ‘‘


⭐ APPLICATION USAGE

these procedures commonly show up when you're implementing algorithms from coding theory. so like, if you're doing that, you already know. iykyk. πŸ”₯


🧚 RATIONALE

a native implementation of these procedures can be made much faster than one provided by the user. so like, use the built-in one, bestie, it ate. πŸ’€


🌈 COMPATIBILITY

these procedures are new in R6RS. also, there's a similar procedure in SRFI-151 in case you were wondering. πŸ¦‹


😭 ERRORS

this procedure can raise exceptions with the following condition types β€” pay attention, this is the serious part:

  • &assertion (R6RS) β€” the wrong number of arguments was passed, or an argument was outside its domain. In particular, start and end must be non-negative, and start must be less than or equal to end.

For the fixnum variant, in addition to the above, the arguments must be fixnums, and end must be less than the fixnum width. See fixnum-width(3scm).

like, do not skip the domain check, i'm begging πŸ™ˆ

πŸ’œ SEE ALSO

go read these next, bestie, they're the whole friend group:


πŸ’– STANDARDS

R6RS


πŸ“ HISTORY

First appeared in R6RS. so she's not new new, she's classic. ⭐


πŸ‘‘ 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/. 🫢


πŸ’€ BUGS

okay this one matters: programs should be careful to limit the end argument of the generic procedure to a reasonable value to prevent excessive resource usage. like, don't hand it a cursed giant number and then act surprised. 😭

that's the whole page, bestie β€” no cap, you're now an expert πŸ’…βœ¨

2022-04-12 Β· from the scheme-manpages project, page bitwise-...bit-field(3scm) β€” mwah, go reverse some bits πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from