[erlang-questions] surprising integer (external) encoding

Valentin Micic valentin@REDACTED
Thu Jul 5 21:30:47 CEST 2007


> 4 bits for the type tag.
> 1 bit for sign.

You're talking about *internal* term presentation, right? Because this 
explanation does not hold water for external format, where things are quite 
different. For example:

(A)    term_to_binary( 2 ) produces: <<131, 97, 2>>

where:
    - 131 is a *magic* number
    - 97 represents external tag for one-byte integer
    - 2 represents the integer value, encoded in a single byte.

According to your explanation one would not be able to encode single integer 
values higer that 2^3, and yet,

(B)    term_to_binary( 255 ) produces: <<131, 97, 255>>


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.

How does "4 bits for tag and 1 bit for sign" fit this observation?

V.

PS
Thanks for the reply.




More information about the erlang-questions mailing list