[r6rs-discuss] [Formal] bitwise-bit-count should return -ve on -ve argument.
Scheme at Bawden.Org
Mon Apr 23 21:20:44 EDT 2007
Date: Mon, 23 Apr 2007 23:50:04 +0100 (BST)
From: Ben Harris <bjh21 at bjh21.me.uk>
Cc: discuss at r6rs.org
Ah, yes, I think I got carried away with the cuteness of the idea and
didn't bother to check it was actually true.
And it is a cute idea. My hat's off to you for having the original
> If bitwise-bit-count was defined as:
> (define (bitwise-bit-count n)
> (if (negative? n)
> (- (+ 1 (bitwise-bit-count (bitwise-not n))))
> Then you could count the bits set in an N-bit word by taking
> the bitwise-bit-count mod N+1. This would also make the
> claim in the subject actually true.
Indeed, and it wouldn't break the other nice property I'd produced. It
loses a little symmetry, in that (- (bitwise-bit-count x)) is no longer
equal to (bitwise-bit-count (bitwise-not x)), but two's-complement
arithmetic is inherently asymmetric like this, so I'm not sure that's a
You want symmetry? I'll give you symmetry... Under the revised definition
(bitwise-not (bitwise-bit-count x)) is equal to
(bitwise-bit-count (bitwise-not x))!
In fact I could have written the definition this way:
(define (bitwise-bit-count n)
(if (negative? n)
(bitwise-not (bitwise-bit-count (bitwise-not n)))
More information about the r6rs-discuss