Skip to content

Instantly share code, notes, and snippets.

@cvalarida
Last active August 7, 2018 03:59
Show Gist options
  • Save cvalarida/1a808488708766d7e7bce524de0651ad to your computer and use it in GitHub Desktop.
Save cvalarida/1a808488708766d7e7bce524de0651ad to your computer and use it in GitHub Desktop.
An Elisp attempt at a little engineering problem--converting a decimal number to binary.
(defun dec-to-bin (dec)
(interactive "nDecimal number to convert: ")
(setq debug-on-error t)
(let ((bin-string "")
(current-dec dec))
(while (/= 0 current-dec)
(setq bin-string (concat (number-to-string (% current-dec 2)) bin-string))
(setq current-dec (truncate (/ current-dec 2))))
(message "%d in binary is %s" dec bin-string)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment