[erlang-questions] surprising integer (external) encoding
Thu Jul 5 23:59:16 CEST 2007
On 7/5/07, Valentin Micic <> wrote:
> To make things even more confusing, the second complement encoding is using
> always 32, i.e.
> (C) term_to_binary( -2 ) produces: <<131,98,255,255,255,254>>
> - 131 is a *magic* number
> - 98 represents external tag for four-byte integer (actually, 2^27)
> - <<255,255,255,254>> -- second complement encoding for -2.
According to the code in erts/emulator/beam/external.c:
* For backward compatibility reasons, only encode integers that
* fit in 28 bits (signed) using INTEGER_EXT.
#define IS_SSMALL28(x) (((Uint) (((x) >> (28-1)) + 1)) < 2)
Since (x) here isn't cast to an unsigned, this check doesn't work for
negative numbers. So it's apparently the combination of historical
reasons and a bug.
I have no idea what the historical reasons are, but I found your
More information about the erlang-questions