[erlang-questions] bit syntax: what does "<<1234>> =" match?

Anton Lebedevich <>
Sat Feb 26 17:37:27 CET 2011


On 02/26/2011 05:24 PM, Anthony Ramine wrote:
> Le 25 févr. 2011 à 20:23, Anton Lebedevich a écrit :
> 
>> Hello,
>> 
>> In erlang R14B01 shell: 
>>1> <<1234>> = <<1234>>. ** exception error: no match of right hand
side value <<"Ò">>
>> 
>> Right side of expression evaluates to <<210>> (8 least significant
>> bits from 1234), so the following matches: 
>> 2> <<210>> = <<1234>>.
>> <<"Ò">>
>> 
>> 1234 on the left side of expression should evaluate to <<210>> too,
>> but it doesn't: 
>> 3> <<1234>> = <<210>>. ** exception error: no match of right hand
side value <<"Ò">>
>> 
>> Is there an explanation of what happens when integer on left side
>> of binary matching expression doesn't fit into default 8 bit?
>> 
>> Regards, Anton Lebedevich.
> 
> Wild guess here, but I would say <<1234>> on the right side
> constructs a binary using the use-only-the-8-least-significant-bits,
> thus not failing because the integer is too large; while <<1234>> on
> the left side is a pattern, so the integer isn't truncated and
> obviously fails to match since you can't have a byte larger than
> 255.

Yes, <<1234>> on the right side becomes <<210>>, but I can't figure out
what happens with <<1234> on the left side. Obviously it isn't truncated
to <<210>> (least significant 8 bits), it doesn't fit into 16 bit too:
1> <<1234>> = <<1234:16>>.
** exception error: no match of right hand side value <<4,210>>

Regards,
Anton Lebedevich.



More information about the erlang-questions mailing list