Skip to content

Instantly share code, notes, and snippets.

@gausby
Created September 15, 2016 09:21
Show Gist options
  • Save gausby/20676a9452bece37a43afff7e28e5f44 to your computer and use it in GitHub Desktop.
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
# ...
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