[erlang-questions] map over bitstring

Tony Rogvall <>
Wed Oct 27 21:15:27 CEST 2010


I love the one-liners (can't help it)

Close to what you need?

<< <<(X-$0):1>> || <<X>> <= <<$1,$0,$0,$1,$0,$0,$1,$0>> >>.

How many bits did you need ;-)

8, 9 or 10 ?

<< 292 >> is truncated into <<36>> (or <<"$">>) since it's more bits than 8.

Ps
	Start an Erlang shell (erl) and poke your expression in, and see what the results are.

/Tony


On 27 okt 2010, at 20.24, rgowka1 wrote:

> Hi -
> 
> How can I convert a bitstring that has only characters $1 and $0 into a binary.
> 
> <<0,1,0,0,1,0,0,1,0>> to << 2#0100100100 >> to << 292 >>
> 
> something similar to io_lib:fread("~2u", [$0,$1,$0,$0,$1,$0,$0,$1,$0])
> but that works on bitstring instead of string.
> 
> thanks.
> 
> On Mon, Oct 25, 2010 at 6:23 AM, Morten Krogh <> wrote:
>> Kostis,
>> 
>> I followed your suggestion and compiled it to native code with erlc +native
>> zip2.erl, which improves the runtime for
>> both methods.
>> 
>> The run times are, for a 100 MB binary, and the map function X -> X + 1 hard
>> coded into the module and into the map method itself (which is not quite
>> realistic):
>> 
>> Not native:
>> 
>> comprehension :     10 s
>> my own bit syntax function : 14s
>> 
>> native:
>> 
>> comprehension: 3.7 s
>> my own bit syntax function: 6 s
>> 
>> So, yes native is an improvement!
>> 
>> Morten.
>> 
>> 
>> 
>> On 10/25/10 11:30 AM, Kostis Sagonas wrote:
>>> 
>>> Morten Krogh wrote:
>>>> 
>>>> Sorry, in my recent reply to Tony I tested the comprehension on the
>>>> constant ><<"B"">>.
>>>> 
>>>> Putting everything in the module and comparing the same mapped function
>>>> ,f , the result is that the comprehension is 30% faster than my matching
>>>> based map function.
>>> 
>>> Morten,
>>> 
>>> You may also want to try compiling the file to native code and report
>>> whether or how this improves/changes the results.
>>> 
>>> Cheers,
>>> Kostis
>> 
>> 
>> ________________________________________________________________
>> erlang-questions (at) erlang.org mailing list.
>> See http://www.erlang.org/faq.html
>> To unsubscribe; mailto:
>> 
>> 
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
> 

"Have run Make so many times I dunno what's installed anymore"



More information about the erlang-questions mailing list