[r6rs-discuss] [Formal] bitwise-bit-count should return -ve on -ve argument.

From: Alan Bawden <Scheme>
Date: Mon, 23 Apr 2007 21:20:44 -0400 (EDT)

   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
inspiration!

> If bitwise-bit-count was defined as:
>
> (define (bitwise-bit-count n)
> (if (negative? n)
> (- (+ 1 (bitwise-bit-count (bitwise-not n))))
> <whatever>))
>
> 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
   bad thing.

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)))
         <whatever>))

- Alan
Received on Mon Apr 23 2007 - 21:20:44 UTC

This archive was generated by hypermail 2.3.0 : Wed Oct 23 2024 - 09:15:01 UTC