Bit twiddling

Torbjorn Tornkvist tobbe@REDACTED
Fri Apr 1 00:48:32 CEST 2005


Joel Reymont wrote:

>Folks,
>
>What's the most effective way to do bit twiddling in Erlang? 
>
>Should I convert the int into a list of bits? How would I efficiently do this?
>
>I'm looking to figure out the highest and lowest bit set in an integer as
>well as count the number of bits set.
>
>    Thanks, Joel
>
>  
>
I'm not sure how efficient this is, but its an alternative
to the solutions Thomas sent:
--------------
Eshell V5.4.5  (abort with ^G)
1>  I=2#10100101.
165
2> <<A:1,B:1,C:1,D:1,E:1,F:1,G:1,H:1>> = <<I:8>>.
<<165>>
3> lists:foldl(fun(N,Acc) -> N+Acc end, 0, [A,B,C,D,E,F,G,H]).
4
-------------

Cheers, Tobbe




More information about the erlang-questions mailing list