Skip to content

Instantly share code, notes, and snippets.

@marcoheisig
Created October 4, 2021 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcoheisig/3ec5a60feffc160f3ac49d53827e592f to your computer and use it in GitHub Desktop.
Save marcoheisig/3ec5a60feffc160f3ac49d53827e592f to your computer and use it in GitHub Desktop.
Extract the odd bits of an integer.
(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