-
Notifications
You must be signed in to change notification settings - Fork 13
/
bits.clj
67 lines (60 loc) · 2.71 KB
/
bits.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
; Copyright (c) Alan Thompson. All rights reserved.
; The use and distribution terms for this software are covered by the Eclipse Public License 1.0
; (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the file epl-v10.html at
; the root of this distribution. By using this software in any fashion, you are agreeing to be
; bound by the terms of this license. You must not remove this notice, or any other, from this
; software.
(ns tupelo.bits
"Tupelo - Making Clojure even sweeter"
(:use tupelo.core)
(:require
[clojure.string :as str]
[schema.core :as s]
))
(s/defn bit->char :- Character
"Given an integer bit value 0 or 1, returns a character '0' or '1'"
[bit-val :- s/Int]
(cond
(= bit-val 0) \0
(= bit-val 1) \1
:else (throw (ex-info "Illegal bit value" (vals->map bit-val)))))
(s/defn char->bit :- s/Int
"Given a character '0' or '1', returns an integer bit value 0 or 1"
[bit-char :- Character]
(cond
(= bit-char \0) 0
(= bit-char \1) 1
:else (throw (ex-info "Illegal bit char" (vals->map bit-char)))))
;-----------------------------------------------------------------------------
(s/defn long->bits-unsigned :- [s/Int]
"Given a positive integer value, returns a 64-len vector of 0/1 integer values"
[long-val :- s/Int]
(when-not (<= 0 long-val Long/MAX_VALUE)
(throw (ex-info "value out of range [0..Long/MAX_VALUE]" (vals->map long-val))))
(let [binary-chars (Long/toBinaryString long-val)
bits (mapv char->bit binary-chars)
nbits (count bits)
leading-zeros (repeat (- 64 nbits) 0)
result (glue leading-zeros bits)]
result))
(s/defn bits-unsigned->long :- s/Int
"Given a vector of 0/1 integer bit values, returns a signed long value"
[bits :- [s/Int]]
(let [binary-chars (prepend \0 (mapv bit->char bits))
binary-str (str/join binary-chars)
result (Long/valueOf binary-str 2)] ; cannot return neg value unless has '-' sign
result))
;-----------------------------------------------------------------------------
(s/defn byte->bits-unsigned :- [s/Int]
"Given a positive integer value, returns an 8-len vector of 0/1 integer values"
[byte-val :- s/Int]
(when-not (<= 0 byte-val Byte/MAX_VALUE)
(throw (ex-info "value out of range [0..Byte/MAX_VALUE]" (vals->map byte-val))))
(vec (take-last 8 (long->bits-unsigned byte-val))))
(s/defn bits-unsigned->byte :- s/Int
"Given a vector of 0/1 integer bit values, returns a signed byte value"
[bits :- [s/Int]]
(let [binary-chars (mapv bit->char bits)
binary-str (str/join binary-chars)
result (Byte/valueOf binary-str 2)] ; cannot return neg value unless has '-' sign
result))