Created
November 3, 2016 21:55
-
-
Save frenchy64/dea6499ccc693e1ee3db2eae302e9e85 to your computer and use it in GitHub Desktop.
Bit manipulation in Cloure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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