bitwise-length(3scm)
π bitwise-length(3scm) & fxlength π
πΈ NAME πΈ
bitwise-length, fxlength β bitwise length of an integer. This is the manual page for bitwise-length(3scm), bestie π
π LIBRARY π
okay so the import vibe check is this:
(import (rnrs)) ;R6RS
(import (rnrs arithmetic bitwise)) ;R6RS
(import (rnrs arithmetic fixnums)) ;R6RS
β¨ SYNOPSIS β¨
(bitwise-length ei)
(fxlength fx)
π¦ DESCRIPTION π¦
So here's the tea π: bitwise-length returns the number of bits needed to represent ei if it is positive, and it returns the number of bits needed to represent (bitwise-not ei) if it is negative.
The fxlength variant constrains the argument and return value to the fixnum range, periodt.
π IMPLEMENTATION NOTES π
ngl there are no noteworthy differences between implementations of these procedures, apart from performance differences. R6RS gives this standard definition:
(do ((result 0 (+ result 1))
(bits (if (negative? ei)
(bitwise-not ei)
ei)
(bitwise-arithmetic-shift bits -1)))
((zero? bits)
result))
This implementation runs in O(n) where n is the bitwise length of the input. An implementation which is O(1) is possible if the implementer has access to the internal structure of bignums. Furthermore, CPUs often provide instructions that speed up the fixnum case. highkey iconic runtime behavior π
π RETURN VALUES π
Returns a single exact integer object. that's it, that's the return π
π EXAMPLES π
(bitwise-length #b1000)
=> 4
(fxlength #xff)
=> 8
(fxlength -1)
=> 0
(fxlength #b-1000)
=> 3
(bitwise-length #x-70)
=> 7
π§ APPLICATION USAGE π§
This procedure is commonly used to compute the number bytes needed to represent an integer during serialization to an external format. literally serialization bestie behavior π
β RATIONALE β
This procedure can be implemented more efficiently by the Scheme runtime than by portable code, and it has some use in applications. so yes, it's giving useful and optimized β¨
π COMPATIBILITY π
These procedures are specified by R6RS. The generic variant is called integer-length in Common Lisp, SRFI 33, SRFI 60 and SRFI 151.
The usual compatibility concerns surrounding fixnums apply to the fixnum variant. no cap, keep that in mind π«Ά
π 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.
π₯ SEE ALSO π₯
bitwise-first-bit-set(3scm)log(3scm)
π STANDARDS π
R6RS
π¦ HISTORY π¦
MacLisp had an equivalent procedure called haulong. cute little historical fact fr fr π
π 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/.
π okay that's the whole bitwise-length(3scm) page, bestie β from the Scheme Programmer's Manual, dated 2021-06-12. go count those bits and 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