bitwise-reverse-bit-field(3scm) Scheme Programmer's Manual bitwise-reverse-bit-field(3scm)

bitwise-reverse-bit-field, fxreverse-bit-field - reverse bit order in an integer

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

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

Returns the integer obtained by by reversing the order of the bits in n at positions from start (inclusive) to end (exclusive).

Returns a single value; an integer.

(bitwise-reverse-bit-field #b0011 0 4)
                           ;   ||
                           ;   vv
                           => #b1100
(bitwise-reverse-bit-field #xAA 0 8)
        => #x55
(bitwise-reverse-bit-field #xAABBCCDD 24 32)
        => #x55BBCCDD

These procedures commonly show up when implementing algorithms from coding theory.

A native implementation of these procedures can be made much faster than one provided by the user.

These procedures are new in R6RS. There is a similar procedure in SRFI-151.

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. 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).

bitwise-rotate-bit-field(3scm), bitwise-arithmetic-shift(3scm), bitwise-copy-bit-field(3scm)

R6RS

First appeared in R6RS.

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://weinholt.se/scheme/manpages/.

Programs should be careful to limit the end argument of the generic procedure to a reasonable value to prevent excessive resource usage.
2022-04-12