bitwise-bit-field(3scm)
π bitwise-bit-field β the scheme programmer's manual page, but make it cute
okay bestie, so this is bitwise-bit-field and fxbit-field from the Scheme Programmer's Manual, and honestly? these two are little scissors for integers. they snip out a chunk of bits and hand it to you like a lil gift π
β¨ NAME β¨
bitwise-bit-field, fxbit-field β bit field extract π§
π LIBRARY π
you can import these bad boys from three different places, choose ur fighter:
(import (rnrs)) ;R6RS
(import (rnrs arithmetic bitwise)) ;R6RS
(import (rnrs arithmetic fixnums)) ;R6RS
β¨ SYNOPSIS β¨
(bitwise-bit-field ei start end)
(fxbit-field fx start end)
π DESCRIPTION π
So what it does is: it returns the number represented by the bits at the positions from start (inclusive!) to end (exclusive!), which is literally the result of the following computation. no cap, this is the exact definition:
;; bitwise-bit-field
(let ((mask (bitwise-not
(bitwise-arithmetic-shift-left -1 end))))
(bitwise-arithmetic-shift-right (bitwise-and ei mask)
start))
;; fxbit-field
(let ((mask (fxnot
(fxarithmetic-shift-left -1 end))))
(fxarithmetic-shift-right (fxand fx mask)
start))
like, it builds a mask, ANDs it, shifts it, done. iconic behaviour honestly π₯
πΈ RETURN VALUES πΈ
Returns a single exact integer object. one (1) exact integer. that's it. that's the return value π
π¦ EXAMPLES π¦
watch it work, bestie:
(fxbit-field #b10101111 4 8)
=> #b1010
(fxbit-field #b10101111 0 4)
=> #b1111
ate and left no crumbs β¨
π APPLICATION USAGE π
The operation implemented by these procedures shows up when several fields are encoded in a single integer. A typical example is the code in a Scheme implementation that parses binary IEEE 754 floating point. This pattern also appears often in binary file formats, network protocols, machine code, encryption and much more.
so likeβ¦ if you're ever decoding a float by hand or staring at a network packet like ποΈπποΈ β this is the vibe. iykyk.
π COMPATIBILITY π
These procedures are new in R6RS. π
SRFI-151 has the generic procedure under the name bit-field.
π ERRORS π
These procedures can raise exceptions with the following condition types:
&assertion(R6RS) β The wrong number of arguments was passed or an argument was outside its domain. The argumentsstartandendmust be non-negative andstartmust be less than or equal toend. Thefxbit-fieldprocedure requires that all arguments are fixnums.
ok so fr fr: give it bad numbers and it will complain.startβ€end, both non-negative, andfxbit-fieldis picky β everything has to be a fixnum. no exceptionsβ¦ well, except the exceptions. π
π«Ά SEE ALSO π«Ά
β STANDARDS β
R6RS
π 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/.
k bye bestie!! this page is from the scheme-manpages project, dated 2021-03-11 β go read more at the url above, mwah ππβ¨
2021-03-11 Β· bitwise-bit-field(3scm) Β· slay πΈ
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2021 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms