1;;;; bitwise-33 - Olin Shivers's code from SRFI-33 with modified names23;;; Olin Shivers is the sole author of this code, and he has placed it in4;;; the public domain.5;;;6;;; A good implementation might choose to provide direct compiler/interpreter7;;; support for these derived functions, or might simply define them to be8;;; integrable -- i.e., inline-expanded.9;;;10;;; The seven non-trivial boolean functions in terms11;;; of not, and, or & xor.1213(define (bitwise-nand i j) (bitwise-not (bitwise-and i j)))14(define (bitwise-nor i j) (bitwise-not (bitwise-ior i j)))15(define (bitwise-andc1 i j) (bitwise-and (bitwise-not i) j))16(define (bitwise-andc2 i j) (bitwise-and i (bitwise-not j)))17(define (bitwise-orc1 i j) (bitwise-ior (bitwise-not i) j))18(define (bitwise-orc2 i j) (bitwise-ior i (bitwise-not j)))1920;;; This is a general definition, but less than efficient. It should also21;;; receive primitive compiler/interpreter support so that the expensive22;;; n-ary mechanism is not invoked in the standard cases -- that is,23;;; an application of BITWISE-EQV should be rewritten into an equivalent24;;; tree applying some two-argument primitive to the arguments, in the25;;; same manner that statically-known n-ary applications of associative26;;; operations such as + and * are handled efficiently:27;;; (bitwise-eqv) => -128;;; (bitwise-eqv i) => i29;;; (bitwise-eqv i j) => (%bitwise-eqv i j)30;;; (bitwise-eqv i j k) => (%bitwise-eqv (%bitwise-eqv i j) k)31;;; (bitwise-eqv i j k l) => (%bitwise-eqv (%bitwise-eqv (%bitwise-eqv i j) k) l)3233(define (bitwise-eqv . args)34 (let lp ((args args) (ans -1))35 (if (pair? args)36 (lp (cdr args) (bitwise-not (bitwise-xor ans (car args))))37 ans)))3839;;; Helper function -- make a mask of SIZE 1-bits, e.g. (%MASK 3) = #b111.40;;; Suppose your Scheme's fixnums are N bits wide (counting the sign bit,41;;; not counting any tag bits). This version, due to Marc Feeley, will42;;; handle SIZE in the range [0,N-1] without overflowing to bignums.43;;; (For SIZE >= N, the correct bignum value is also produced.)4445(define (mask start end) (bitwise-not (arithmetic-shift -1 (- end start))))4647;;; This alternate, mathematically-equivalent expression48;;; (- (arithmetic-shift 1 size) 1)49;;; is not as good -- it only handles SIZE in the range [0,N-2] without50;;; overflowing to bignums.51;;;52;;; Finally, note that even Feeley's expression can't build an N-bit mask53;;; without bignum help. This is fundamental, since the interpretation54;;; of fixed-size fixnum bit patterns as semi-infinite-bit-strings is that55;;; you replicate the high bit out to infinity. So you have to have a56;;; zero "stop bit" appearing after that highest one bit to turn off the57;;; replication of the ones.5859(define (bit-set? index n)60 (not (zero? (bitwise-and (arithmetic-shift 1 index) n))))6162(define (any-bit-set? test-bits n) (not (zero? (bitwise-and test-bits n))))6364(define (every-bit-set? test-bits n) (= test-bits (bitwise-and test-bits n)))6566;;; Bit-field ops6768(define (bit-field n start end)69 (bitwise-and (mask start end) (arithmetic-shift n (- start))))7071(define (bit-field-any? n start end)72 (not (zero? (bitwise-and (arithmetic-shift n (- start)) (mask start end)))))7374;; Part of Olin's late revisions; code by John Cowan; public domain.75(define (bit-field-every? n start end)76 (let ((m (mask start end)))77 (eqv? m (bitwise-and (arithmetic-shift n (- start)) m))))7879;; Integrating i-b-f reduces nicely.80(define (bit-field-clear n start end)81 (bit-field-replace n 0 start end))8283;; Counterpart to above, not in SRFI 33, written by John Cowan, public domain84(define (bit-field-set n start end)85 (bit-field-replace n -1 start end))8687;;; Oops -- intermediate ARITHMETIC-SHIFT can fixnum-overflow on fixnum args.88;(define (bit-field-replace newfield n start end)89; (bit-field-replace-same (arithmetic-shift newfield start) n start end))9091;;; This three-line version won't fixnum-overflow on fixnum args.92(define (bit-field-replace n newfield start end)93 (let ((m (mask start end)))94 (bitwise-ior (bitwise-and n (bitwise-not (arithmetic-shift m start)))95 (arithmetic-shift (bitwise-and newfield m) start))))9697(define (bit-field-replace-same to from start end)98 (bitwise-if (arithmetic-shift (mask start end) start) from to))99100;; Simple definition101;(define (first-set-bit i)102; (and (not (zero? i))103; (let lp ((j 0) (i start))104; (if (bit-set? i 0) j105; (lp (+ j 1) (arithmetic-shift i 1))))))106107;;; Clever definition, assuming you have a fast BIT-COUNT.108(define (first-set-bit i) (- (bit-count (bitwise-xor i (- i 1))) 1))109