Skip to content

Instantly share code, notes, and snippets.

@basp1
Created May 11, 2016 19:07
Show Gist options
  • Save basp1/96d857f45593991e3bd76ff76582127e to your computer and use it in GitHub Desktop.
Save basp1/96d857f45593991e3bd76ff76582127e to your computer and use it in GitHub Desktop.
(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