(defun f (x &optional (length 1))
(if (integerp x)
(loop with bits = '()
for (q r) = (multiple-value-list (floor x 2))
then (multiple-value-list (floor q 2))
repeat (max length (1+ (integer-length x)))
do (push r bits)
finally (return (if (and (eql x -1) (eql length 1))
(cons 1 bits)
bits)))
(destructuring-bind (sign-bit . bits) x
(loop for b in (if (zerop sign-bit)
bits
(mapcar (lambda (x) (logxor x 1)) bits))
for i downfrom (1- (length bits))
sum (* b (expt 2 i)) into n
finally (return (if (zerop sign-bit) n (- (1+ n))))))))
(loop for args in '((10)
(10 8)
(-8)
(-8 8)
(-1)
nil
((0 1 1 1))
((1 1 1 1))
((1 0 0 0))
nil
(1)
(1 1)
(1 2)
(-1)
(-1 1)
(-1 2)
nil
((0 1))
((1))
((1 1))
nil
(0)
((0)))
do (if args
(format t "~{~S~^, ~}~10T-> ~S~%" args (apply #'f args))
(terpri)))
KGRlZnVuIGYgKHggJm9wdGlvbmFsIChsZW5ndGggMSkpCiAgKGlmIChpbnRlZ2VycCB4KQogICAgICAobG9vcCB3aXRoIGJpdHMgPSAnKCkKICAgICAgICAgICAgZm9yIChxIHIpID0gKG11bHRpcGxlLXZhbHVlLWxpc3QgKGZsb29yIHggMikpCiAgICAgICAgICAgIHRoZW4gKG11bHRpcGxlLXZhbHVlLWxpc3QgKGZsb29yIHEgMikpCiAgICAgICAgICAgIHJlcGVhdCAobWF4IGxlbmd0aCAoMSsgKGludGVnZXItbGVuZ3RoIHgpKSkKICAgICAgICAgICAgZG8gKHB1c2ggciBiaXRzKQogICAgICAgICAgICBmaW5hbGx5IChyZXR1cm4gKGlmIChhbmQgKGVxbCB4IC0xKSAoZXFsIGxlbmd0aCAxKSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAoY29ucyAxIGJpdHMpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYml0cykpKQogICAgICAoZGVzdHJ1Y3R1cmluZy1iaW5kIChzaWduLWJpdCAuIGJpdHMpIHgKICAgICAgICAobG9vcCBmb3IgYiBpbiAoaWYgKHplcm9wIHNpZ24tYml0KQogICAgICAgICAgICAgICAgICAgICAgICAgICBiaXRzCiAgICAgICAgICAgICAgICAgICAgICAgICAgIChtYXBjYXIgKGxhbWJkYSAoeCkgKGxvZ3hvciB4IDEpKSBiaXRzKSkKICAgICAgICAgICAgICBmb3IgaSBkb3duZnJvbSAoMS0gKGxlbmd0aCBiaXRzKSkKICAgICAgICAgICAgICBzdW0gKCogYiAoZXhwdCAyIGkpKSBpbnRvIG4KICAgICAgICAgICAgICBmaW5hbGx5IChyZXR1cm4gKGlmICh6ZXJvcCBzaWduLWJpdCkgbiAoLSAoMSsgbikpKSkpKSkpCgoobG9vcCBmb3IgYXJncyBpbiAnKCgxMCkKICAgICAgICAgICAgICAgICAgICAoMTAgOCkKICAgICAgICAgICAgICAgICAgICAoLTgpCiAgICAgICAgICAgICAgICAgICAgKC04IDgpCiAgICAgICAgICAgICAgICAgICAgKC0xKQogICAgICAgICAgICAgICAgICAgIG5pbAogICAgICAgICAgICAgICAgICAgICgoMCAxIDEgMSkpCiAgICAgICAgICAgICAgICAgICAgKCgxIDEgMSAxKSkKICAgICAgICAgICAgICAgICAgICAoKDEgMCAwIDApKQogICAgICAgICAgICAgICAgICAgIG5pbAogICAgICAgICAgICAgICAgICAgICgxKQogICAgICAgICAgICAgICAgICAgICgxIDEpCiAgICAgICAgICAgICAgICAgICAgKDEgMikKICAgICAgICAgICAgICAgICAgICAoLTEpCiAgICAgICAgICAgICAgICAgICAgKC0xIDEpCiAgICAgICAgICAgICAgICAgICAgKC0xIDIpCiAgICAgICAgICAgICAgICAgICAgbmlsCiAgICAgICAgICAgICAgICAgICAgKCgwIDEpKQogICAgICAgICAgICAgICAgICAgICgoMSkpCiAgICAgICAgICAgICAgICAgICAgKCgxIDEpKQogICAgICAgICAgICAgICAgICAgIG5pbAogICAgICAgICAgICAgICAgICAgICgwKQogICAgICAgICAgICAgICAgICAgICgoMCkpKQogICAgICBkbyAoaWYgYXJncwogICAgICAgICAgICAgKGZvcm1hdCB0ICJ+e35Tfl4sIH59fjEwVC0+IH5TfiUiIGFyZ3MgKGFwcGx5ICMnZiBhcmdzKSkKICAgICAgICAgICAgICh0ZXJwcmkpKSkK
10 -> (0 1 0 1 0)
10, 8 -> (0 0 0 0 1 0 1 0)
-8 -> (1 0 0 0)
-8, 8 -> (1 1 1 1 1 0 0 0)
-1 -> (1 1)
(0 1 1 1) -> 7
(1 1 1 1) -> -1
(1 0 0 0) -> -8
1 -> (0 1)
1, 1 -> (0 1)
1, 2 -> (0 1)
-1 -> (1 1)
-1, 1 -> (1 1)
-1, 2 -> (1 1)
(0 1) -> 1
(1) -> -1
(1 1) -> -1
0 -> (0)
(0) -> 0