Liking cljdoc? Tell your friends :D

# bits

A smallish Clojure library for transforming Java primitives into binary sequences and back again. Intended to help explore encodings. Particularly useful for `float` and `double` types.

## Quick Demo

``````(ns whose.ns
(:require [com.evanjbowling.bits :as bits]))
``````

View the binary encoding for a few `short`s:

``````(->> ["7" "0" "-1"]
(map bits/short-bits)
clojure.pprint/pprint)

; ((0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1)
;  (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
;  (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1))
``````

Examine some `char`s:

``````(->> ["A" "a" "µ"]
(map bits/char-bits)
clojure.pprint/pprint)

; ((0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1)
;  (0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1)
;  (0 0 0 0 0 0 0 0 1 0 1 1 0 1 0 1))
``````

The floating point bit sequences are grouped [[sign] [exponent] [fraction]]:

``````(bits/float-bits "0.25")

; ([0] [0 1 1 1 1 1 0 1] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0])
``````

You can convert the bit sequence back to it's original form:

``````(bits/from-float-bits '([0] [0 1 1 1 1 1 0 1] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]))

; 0.25
``````

This is a great way to see what happens when you make the exponent all ones:

``````(bits/from-float-bits '([0] [1 1 1 1 1 1 1 1] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]))

; Infinity
``````

And then tweak the fraction bits too:

``````(bits/from-float-bits '([0] [1 1 1 1 1 1 1 1] [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]))

; NaN
``````