Created
October 4, 2021 14:12
-
-
Save marcoheisig/3ec5a60feffc160f3ac49d53827e592f to your computer and use it in GitHub Desktop.
Extract the odd bits of an integer.
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 odd-bits (x) | |
(declare (type (unsigned-byte 32) x)) | |
(setf x (logand (ash x -1) #x55555555)) | |
(setf x (logand (logior (ash x -1) x) #x33333333)) | |
(setf x (logand (logior (ash x -2) x) #x0F0F0F0F)) | |
(setf x (logand (logior (ash x -4) x) #x00FF00FF)) | |
(setf x (logand (logior (ash x -8) x) #x0000FFFF)) | |
x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment