[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: C-x C-e on numbers
From: |
Stephen J. Turnbull |
Subject: |
Re: C-x C-e on numbers |
Date: |
Fri, 05 Dec 2014 15:53:36 +0900 |
Stephen Leake writes:
> M-: (show-binary 42)
(defun show-binary (n)
"Documentation left as exercise for the reader."
;; hexadecimal version also an exercise
(let ((s (format "%o" n)))
;; loop unrolled for creativity conservation
(setq s (replace-in-string s "0" "000"))
(setq s (replace-in-string s "1" "001"))
(setq s (replace-in-string s "2" "010"))
(setq s (replace-in-string s "3" "011"))
(setq s (replace-in-string s "4" "100"))
(setq s (replace-in-string s "5" "101"))
(setq s (replace-in-string s "6" "110"))
(setq s (replace-in-string s "7" "111"))
;; stripping leading zeros or padding to wordsize, more homework
(concat "#b" s)))
Seriously, does this function really need to be particularly efficient?