/usr/lib/s9fes/integer-to-binary-string.scm is in scheme9 2010.11.13-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | ; Scheme 9 from Empty Space, Function Library
; By Nils M Holm, 2010
; See the LICENSE file of the S9fES package for terms of use
;
; (binary-string->integer string) ==> integer
; (integer->binary-string integer1 integer2) ==> string
; (number-of-bits integer) ==> integer
;
; INTEGER->BINARY-STRING writes a binary notation of INTEGER
; to a fresh string and returns it. The resulting string will
; be INTEGER2 characters wide and will consist of zeroes and
; ones exclusively. INTEGER2 must be large enough to hold the
; desired binary representation. NUMBER-OF-BITS may be used
; to find this value.
;
; BINARY-STRING->INTEGER converts the binary representation of
; an integer contained in STRING to an integer. It basically
; does the same as STRING->NUMBER with a radix of 2, but is
; hopefully more efficient.
;
; NUMBER-OF-BITS returns the least number of bits that is
; needed to store the given integer in binary format.
;
; Example: (integer->binary-string 123 8) ==> "01111011"
; (binary-string->integer "01111011") ==> 123
; (number-of-bits 127) ==> 7
(define (integer->binary-string v k)
(let ((s (make-string k #\0)))
(do ((v v (quotient v 2))
(i (- k 1) (- i 1)))
((zero? v)
s)
(if (odd? v)
(if (negative? i)
(error "integer->binary-string: string too small")
(string-set! s i #\1))))))
(define (binary-string->integer s)
(let ((k (string-length s)))
(do ((v 0 (* 2 v))
(i 0 (+ 1 i)))
((>= i k)
(quotient v 2))
(if (char=? #\1 (string-ref s i))
(set! v (+ 1 v))))))
(define (number-of-bits x)
(let loop ((i 1)
(v 1))
(if (< v x)
(loop (+ 1 i) (+ 1 (* 2 v)))
i)))
|