-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshalling.scm
48 lines (39 loc) · 1.04 KB
/
marshalling.scm
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
(define bit-length 8)
(define (clear-nth-bit n i)
(logand n (lognot (ash 1 i))))
(define (set-nth-bit n i)
(logor n (ash 1 i)))
(define (get-nth-bit n i)
(logand (ash n (- i)) 1))
(define (number->bit-list n)
(map (lambda (c) (if (equal? c #\1) 1 0))
(string->list (number->string n 2))))
(define (bit-list->number bs)
(string->number
(list->string
(map (lambda (n) (if (equal? n 1) #\1 #\0))
bs))
2))
(define (list-duplicate n v)
(if (<= n 0)
'()
(cons v (list-duplicate (sub1 n) v))))
(define (list-fill-to xs n v)
(let ([l (length xs)])
(if (>= l n)
xs
(append xs (list-duplicate (- n l) v)))))
(define (number->byte-list n)
(if (equal? n 0)
'(0)
(let loop ([n n])
(if (equal? n 0)
'()
(cons
(logand n (sub1 (ash 1 bit-length)))
(loop (ash n (- bit-length))))))))
(define (byte-list->number ns)
(if (null? ns)
0
(logor (car ns)
(ash (byte-list->number (cdr ns)) bit-length))))