[erlang-questions] compile error with bits unit syntax

litao cheng litaocheng@REDACTED
Mon Jul 21 03:18:10 CEST 2008


Edwin , thanks for your response! :)

2008/7/19 Edwin Fine <emofine@REDACTED>:

> Litao,
>
> I think this is a bug in Erlang R12B-3. Certainly the documentation can be
> misleading, because in Programming Examples (Section 4.6: Matching
> Binaries), it specifically says that this construct is not allowed:
>
> *"Size** must be an integer literal, or a previously bound variable. Note
> that the following is not allowed: *
>
> *foo(N, <<X:N,T/binary>>) ->
>    {X,T}.
> *
>
> *The two occurrences of N are not related. The compiler will complain that
> the N in the size field is unbound." *
>
> That being said, if you rewrite the expression as shown below, it will
> compile (but does not work). If I understand correctly, binary, bits, and
> bitstream are the same, except that the default bit size for binary is 8,
> and for bitstring it is 1. Since you are overriding the default size anyway,
> you can use binary-unit:11 in place of bits-unit:11.
>
> decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) ->
>     [Chan || <<Chan:11>> <- Chans].
>
>
> 103> Chans3 = <<3:5,2:11,3:11,4:11>>.
> <<24,2,0,96,4:6>>
> 104> bb:decode(Chans3).
> N:3, Chans:<<0,64,12,2,0:1>>
> ** exception error: no case clause matching {<<0,64,12,2,0:1>>}
>      in function  bb:'-decode/1-lc$^0/1-0-'/1
> 105>
>
> The code is:
>
> -module(bb).
> -compile([export_all]).
>
> decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) ->
>     io:format("N:~p, Chans:~p~n", [N, Chans]),
>     [Chan || <<Chan:11>> <- Chans].
>
> So even though the programming examples say that you can't use the same N
> in the match, it actually does work, but the list comprehension does not. I
> found out this is because a bitstring generator has to use "<=" and not
> "<-". So the final working code is:
>
> -module(bb).
> -compile([export_all]).
>
> decode(<<N:5,Chans:N/binary-unit:11,_/bits>>) ->
>     [Chan || <<Chan:11>> *<=* Chans].
>
> 118> c(bb).
> {ok,bb}
> 119> Chans3 = <<3:5,2:11,3:11,4:11>>.
> <<24,2,0,96,4:6>>
> 120> bb:decode(Chans3).
> [2,3,4]
>
> Hope this helps.
>
>
>
> 2008/7/18 litao cheng <litaocheng@REDACTED>:
>
>> hi, all.
>> when I read this paper: Programming Efficiently with Binaries and Bit
>> Strings http://www.erlang.se/euc/07/papers/1700Gustafsson.pdf, I
>> encounter a compile error, the code snipes is a example to parse the IS
>> 683-PRL protocol:
>>
>> decode(<<N:5,Chans:N/bits-unit:11,_/bits>>) ->
>>     [Chan || <<Chan:11>> <- Chans].
>>
>> the compiler says:
>>  bit type mismatch (unit) between 11 and 1
>>
>> I read the erlang reference mannual, the bits unit default is 1, I think
>> the unit can be set, why this compile error occur? thank you!
>> my erlang emulator is  5.6.3(R12B-3).
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>
>
>
> --
> The great enemy of the truth is very often not the lie -- deliberate,
> contrived and dishonest, but the myth, persistent, persuasive, and
> unrealistic. Belief in myths allows the comfort of opinion without the
> discomfort of thought.
> John F. Kennedy 35th president of US 1961-1963 (1917 - 1963)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080721/0b537a55/attachment.htm>


More information about the erlang-questions mailing list