Code: Select all
(define (binary n (width 24) , temp)
(print (format {%6d } n))
(set 'temp '() 'x n)
(if (< n 0) (inc 'x))
(until (= x 0)
(push (string (% (abs x) 2)) temp)
(set 'x (/ x 2)))
(set 'result (format (string {%0} width {s}) (join temp)))
(and
(< n 0)
(replace "1" result "x")
(replace "0" result "1")
(replace "x" result "0"))
(println result))
(for (i -10 10)
(binary i 16))
-10 1111111111110110
-9 1111111111110111
-8 1111111111111000
-7 1111111111111001
-6 1111111111111010
-5 1111111111111011
-4 1111111111111100
-3 1111111111111101
-2 1111111111111110
-1 1111111111111111
0 0000000000000000
1 0000000000000001
2 0000000000000010
3 0000000000000011
4 0000000000000100
5 0000000000000101
6 0000000000000110
7 0000000000000111
8 0000000000001000
9 0000000000001001
10 0000000000001010