(extract-bit-field 1 bit integer))
(define-integrable (bit-set? bit integer)
+ (not (bit-clear? bit integer)))
+
+(define-integrable (bit-clear? bit integer)
(zero? (extract-bit-field 1 bit integer)))
\f
;;; SRFI 60 operations
(lambda (a b)
(if (not (eqv? (hamming-distance a b)
(if (eqv? (negative? a) (negative? b))
- (bit-count (bitwise-xor a b))
- -1)))
+ (bit-count (bitwise-xor a b))
+ -1)))
(error "Failed:" `(HAMMING-DISTANCE ,a ,b)
'=> (hamming-distance a b)))))
-
+\f
(define-test 'BIT-MASK
(lambda ()
(do ((i 0 (+ i 1))) ((>= i #x1000))
(bitwise-not
(shift-left (bitwise-not (shift-left -1 size))
position)))))))
+
+(define (define-per-bit-test name procedure)
+ (define-test name
+ (lambda ()
+ (do ((i 0 (+ i 1))) ((>= i #x100))
+ (procedure (random-integer-of-weight (random-integer #x1000) #x1000)
+ (random-integer #x1000))))))
+
+(define-per-bit-test 'SET-BIT
+ (lambda (n i) (assert-true (bit-set? i (set-bit i n)))))
+
+(define-per-bit-test 'CLEAR-BIT
+ (lambda (n i) (assert-true (bit-clear? i (clear-bit i n)))))
+
+(define-per-bit-test 'TOGGLE-BIT
+ (lambda (n i) (assert-eqv (bit-clear? i (toggle-bit i n)) (bit-set? i n))))
+
+(define-per-bit-test 'EXTRACT-BIT
+ (lambda (n i) (assert-eqv (zero? (extract-bit i n)) (bit-clear? i n))))