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

From: Alan Bawden <Scheme>
Date: Mon, 23 Apr 2007 14:32:13 -0400 (EDT)

   Date: Tue, 17 Apr 2007 21:03:57 +0100 (BST)
   From: Ben Harris <bjh21 at bjh21.me.uk>

   ... Also, for arguments that fit in a particular word
   size, the number of set bits in the argument represented
   in a word of that size can be obtained by taking the
   result modulo the word size, regardless of the sign of
   the argument.

This can't be true. Since the number of bits set in a
32-bit word (for example) can be anything from 0 to 32
-inclusive-, it cannot be true that you can count the set
bits by taking something mod 32.

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.

[ As a member of the Steering Committee I can't really argue
  for or against anything, but this is the kind of
  bit-diddling mathematics that always tickles my fancy, so
  I couldn't help myself! ]
Received on Mon Apr 23 2007 - 14:32:13 UTC

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