bitwise-bit-count - population count
(import (rnrs)) ;R6RS
(import (rnrs arithmetic bitwise)) ;R6RS
(import (rnrs arithmetic fixnums)) ;R6RS
(bitwise-bit-count ei)
(fxbit-count fx)
If the argument is non-negative, this procedure returns the number of 1 bits in
its two's complement representation. Otherwise it returns the result of the
following computation:
(bitwise-not (bitwise-bit-count (bitwise-not ei)))
Returns a single exact integer object.
(bitwise-bit-count #b1)
=> 1
(bitwise-bit-count #b1000)
=> 1
(bitwise-bit-count #b1001)
=> 2
(bitwise-bit-count #b-1001)
=> -2
This procedure is used to compute the Hamming distance. It's also used in Hash
Array Mapped Tries and other applications.
This procedure is new in R6RS. SRFI-151 has the similar procedure
bit-count.
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
- The wrong number of arguments was passed or an argument was outside its
domain. For fxbit-count the argument must be a fixnum.
This procedure can be implemented with the popcount procedure in x86-64
processors. This instruction appeared even in some early computers like the
IBM Stretch (1961).