bit-count

functionsince v0.0-1211Edit
(bit-count v)

(bit-count x)

Counts the number of bits set in x.


Examples:

Bits can be entered using radix notation:

(bit-count 2r1011)
;;=> 3

Same number in decimal:

(bit-count 11)
;;=> 3

Source docstring:
Counts the number of bits set in n
Source code @ clojurescript:src/main/cljs/cljs/core.cljs
(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)))