Skip to content

Instantly share code, notes, and snippets.

@klang
Created May 15, 2013 19:39
Show Gist options
  • Save klang/5586714 to your computer and use it in GitHub Desktop.
Save klang/5586714 to your computer and use it in GitHub Desktop.
I just need the individual bits for something
;; naïve implementation
(defmulti bits type)
(defmethod bits Long [number]
(concat
(repeat (Long/numberOfLeadingZeros number) 0)
(if (not (zero? number))
(->> (Long/toBinaryString number) (map #(Integer/valueOf (str %)))))))
(defmethod bits Integer [number]
(concat
(repeat (Integer/numberOfLeadingZeros number) 0)
(if (not (zero? number))
(->> (Integer/toBinaryString number) (map #(Integer/valueOf (str %)))))))
(defmethod bits Byte [number]
(drop 24 (bits (.intValue number))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment