popcount
Population count (aka hamming weight) function. Counts the number of set (i.e. 1-valued) bits in a 32-bit integer.
Installation
$ npm install @f/popcount
Usage
var popcount = require('@f/popcount')
popcount(3) === 2
popcount(4) === 1
popcount(0xFF) === 8
popcount(0xFF, 3) === 3
API
popcount(x, n)
-
x
- The 32-bit integer who's population you wish to count -
n
- Optional. The bit position to begin the count from. E.g.popcount(x, 8)
returns the number of bits set below the 8th bit inx
.
Returns: The number of bits set in x
below the nth position, if n is specified.
License
MIT