Created
September 15, 2016 09:21
-
-
Save gausby/20676a9452bece37a43afff7e28e5f44 to your computer and use it in GitHub Desktop.
Counting the on bits in a bitfield using really bit integers as the data representation
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
# ... | |
def has(bitfield) do | |
count_elements(bitfield.pieces, 0) | |
end | |
defp count_elements(0, acc), do: acc | |
defp count_elements(pieces, acc) do | |
count_elements(pieces >>> 1, acc + (pieces &&& 1)) | |
end | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment