[erlang-questions] Integer endianness in binaries

Zoltan Lajos Kis <>
Wed Feb 17 23:56:18 CET 2010


Hi,

I think <<15:2/little-integer-unit:7>> simply means: represent integer 
"15" on 14 (2x7) bits, using little endianness (it does not differ from 
<<15:14/little-integer-unit:1>>, <<15:1/little-integer-unit:14>> or 
<<15:7/little-integer-unit:2>>).

With 14 bits your first (lower) byte will be complete, but the second 
(higher) byte will only have 6 bits. 15 can be represented on the lower 
byte alone, hence the result: 00001111 000000.
Note that the bits missing from the last byte are the higher ones; e.g. 
<<513:10/little-integer-unit:1>> is 00000001 10, while 
<<1025:10/little-integer-unit:1>> is 00000001 00 as 1024 is not 
representable on 10 bits.

Z.




Gleb Peregud wrote:
> Hello.
>
> These
>
> [0,0,0,0,0,0,0,1] = [ X || <<X:1>> <= <<1>> ].
> [0,0,0,0,0,0,1,0] = [ X || <<X:1>> <= <<2>> ].
> [0,0,0,0,0,0,1,1] = [ X || <<X:1>> <= <<3>> ].
> [0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0] = [ X || <<X:1>> <=
> <<15:2/little-integer-unit:8>> ].
>
> are true and does not fail. But this:
>
>   
>> [0,0,0,1,1,1,1,0,0,0,0,0,0,0] = [ X || <<X:1>> <= <<15:2/little-integer-unit:7>> ].
>>     
> ** exception error: no match of right hand side value
> [0,0,0,0,1,1,1,1,0,0,0,0,0,0]
>
> fails with given badmatch. Why is it so? Am I missing something?
> Shouldn't <<15:2/little-integer-unit:7>> be the same as <<15:7, 0:7>>
> ? Instead it is equal to <<15:8, 0:6>>.
>
> Best regards,
> Gleb
>
> --
> Gleb Peregud
> Green Elephant Labs
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
>
>   



More information about the erlang-questions mailing list