Skip to content

Instantly share code, notes, and snippets.

@frenchy64
Created November 3, 2016 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frenchy64/dea6499ccc693e1ee3db2eae302e9e85 to your computer and use it in GitHub Desktop.
Save frenchy64/dea6499ccc693e1ee3db2eae302e9e85 to your computer and use it in GitHub Desktop.
Bit manipulation in Cloure
(require '[clojure.pprint :refer [pprint]])
(defn binary-str [bitmap]
(let [s (Integer/toBinaryString bitmap)]
(if (= 32 (count s))
s
(str (apply str (repeat (- 32 (count s)) \0))
s))))
;; get a feel for 2's complement
(pprint (mapv (juxt identity (comp (juxt identity count #(count (filter #{\0} %)) #(count (filter #{\1} %))) binary-str))
(range -512 512)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment