Created
May 11, 2016 19:07
-
-
Save basp1/96d857f45593991e3bd76ff76582127e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun last-set-bit (value) | |
(if (zerop value) | |
-1 | |
(let ((shifted-value (ash value -32))) | |
(if (not (zerop shifted-value)) | |
(+ 32 (last-set-bit shifted-value)) | |
(flet ((stepf (x b) | |
(if (>= value (ash 1 x)) | |
(progn | |
(setf value (ash value (- x))) | |
(+ b x)) | |
b))) | |
(stepf 1 (stepf 2 (stepf 4 (stepf 8 (stepf 16 0)))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment