[erlang-questions] binary elements greater than 255
Kostis Sagonas
kostis@REDACTED
Sat Apr 26 10:15:56 CEST 2008
Zvi wrote:
> Is this a bug?
> ------------------------------------------------------------
> Erlang (BEAM) emulator version 5.6 [smp:2] [async-threads:0]
>
> Eshell V5.6 (abort with ^G)
> 1>
> 1> <<0,255>>.
> <<0,255>>
> 2> <<0,256>>.
> <<0,0>>
> 3> <<0,257>>.
> <<0,1>>
> 4> <<0,65536>>.
> <<0,0>>
No, this is not a bug. This is how binary construction is defined.
The think that confuses you is that when you e.g. write
<<0,256>>
you are most probably forgetting that this is just a shorthand for the
construct
<<0:8/integer-unsigned,256:8/integer-unsigned>>
so you are effectively telling the Erlang system:
I want you to consider 0 and 256 as unsigned integers
take the last 8 bits of each of them and form a 16 bit binary.
Hence the results. This is consistent with
1> <<255:4>>.
<<15:4>>
2> <<-255:4>>.
<<1:4>>
3> <<-1:5>>.
<<31:5>>
Hope this explains things.
Kostis
More information about the erlang-questions
mailing list