bitwise-bit-count - population count

LIBRARY

(import (rnrs))                     ;R6RS
(import (rnrs arithmetic bitwise))  ;R6RS
(import (rnrs arithmetic fixnums))  ;R6RS

SYNOPSIS

(bitwise-bit-count ei)
(fxbit-count fx)

DESCRIPTION

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

RETURN VALUES

Returns a single exact integer object.

EXAMPLES

(bitwise-bit-count #b1)
          => 1

(bitwise-bit-count #b1000)
          => 1

(bitwise-bit-count #b1001)
          => 2

(bitwise-bit-count #b-1001)
          => -2

APPLICATION USAGE

This procedure is used to compute the Hamming distance. It's also used in Hash Array Mapped Tries and other applications.

COMPATIBILITY

This procedure is new in R6RS. SRFI-151 has the similar procedure bit-count.

ERRORS

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.

SEE ALSO

bitwise-first-bit-set(3scm), bitwise-length(3scm)

STANDARDS

R6RS

HISTORY

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

AUTHORS

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/.


Markup created by unroff 1.0sc,    March 04, 2023.