BITWISE-REVERSE-BIT-FIELD(3scm)
π 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,startandendmust be non-negative, andstartmust be less than or equal toend.
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
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2022 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms