function | since v0.0-1211 | Edit |
(bit-count v)
(bit-count x)
Counts the number of bits set in x
.
Bits can be entered using radix notation:
(bit-count 2r1011)
;;=> 3
Same number in decimal:
(bit-count 11)
;;=> 3
Counts the number of bits set in n
(defn bit-count
[v]
(let [v (- v (bit-and (bit-shift-right v 1) 0x55555555))
v (+ (bit-and v 0x33333333) (bit-and (bit-shift-right v 2) 0x33333333))]
(bit-shift-right (* (bit-and (+ v (bit-shift-right v 4)) 0xF0F0F0F) 0x1010101) 24)))