[erlang-questions] surprising bit syntax

Per Gustafsson <>
Tue Jul 3 15:24:27 CEST 2007


Ulf Wiger (TN/EAB) wrote:
> Is it just me, or is this behaviour surprising?
> 
> 4> <<255>>.
> <<"ÿ">>
> 5> <<256>>.
> <<0>>
> 6> <<257>>.
> <<1>>
> 7> <<257:8>>.
> <<1>>
> 8> <<I:8/integer>> = <<999>>.          
> <<"ç">>
> 9> I.
> 231
> 

This behaviour might be surprising and it should probably be mentioned 
in the reference manual that

<<I:N>>

is equivalent to

<<(I band ((1 bsl N) - 1)):N>>

That is, if the integer I can not be represented in N bits the low N 
bits of the integer is put into the binary.

Binaries work in a similar manner e.g.

1> <<1,2>> = << (<<1,2,3>>):2/binary >>.
<<1,2>>

Per




More information about the erlang-questions mailing list