Hi... I've put this short sub for splitting integer into bits, but I am unsure is it correct for negative integers... I know (logand), (logior) functions work and for negative numbers, but I don't understand the logic behind... Any insight is welcome...
;;; bits by M.R.
(defun bits ( n / bit bitl rtn ) ;;; n - integer ; (bits 20000) => (32 512 1024 2048 16384) ; (bits -20000) => (-16384 -2048 -1024 -512 -32) ; (bits 0) => nil
(if (minusp n)
(progn
(setq bit -0.5)
(while (>= (setq bit (fix (* bit 2.0))) n)
(setq bitl (cons bit bitl))
)
(foreach bit (reverse bitl)
(if (= (logand (- bit) (- n)) (- bit))
(setq rtn (cons bit rtn))
)
)
rtn
)
(progn
(setq bit 0.5)
(while (<= (setq bit (fix (* bit 2.0))) n)
(setq bitl (cons bit bitl))
)
(foreach bit (reverse bitl)
(if (= (logand bit n) bit)
(setq rtn (cons bit rtn))
)
)
(reverse rtn)
)
)
)
M.R.