Skip to content

Instantly share code, notes, and snippets.

@alexbowe
Created April 4, 2011 07:01
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 alexbowe/901235 to your computer and use it in GitHub Desktop.
Save alexbowe/901235 to your computer and use it in GitHub Desktop.
Generates a tuple of popcounts.
-module(popcount_table).
-export([gen_table/1]).
gen_table(Bits) -> gen_table(1, 2 bsl (Bits - 1), {0}).
gen_table(Stop, Stop, Table) -> Table;
gen_table(Value, Stop, Table) ->
New = (Value band 1) + erlang:element((Value bsr 1) + 1, Table),
NewTable = erlang:append_element(Table, New),
gen_table(Value + 1, Stop, NewTable).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment