Hello,
First
How can I get the size (bits or bytes) of a variable?
I don’t know if the next are correct:
-
0x002A
4B (if integers are stored in 4B)
-
0x2A00
4B (if integers are stored in 4B)
-
<< 0x002A >>
2B
-
<< 0x2A00::size(16) >>
2B
-
<< 0x2A00::size(8) >>
1B (<< 0x00 >>
)
Second
And, also, Why bytes-size
and size
are not compatible?
@signature << 0xFFD8::size(16) >>
Changed by:
@signature << 0xFFD8::bytes-size(2) >>
Gives me this error:
** (CompileError) lib/file.ex:13: conflicting type specification for bit field: "binary" and "integer"
(elixir) src/elixir_bitstring.erl:87: :elixir_bitstring.expand_specs/5
(elixir) src/elixir_bitstring.erl:29: :elixir_bitstring.expand/6
(elixir) src/elixir_bitstring.erl:12: :elixir_bitstring.expand/4
(stdlib) lists.erl:1354: :lists.mapfoldl/3
(stdlib) lists.erl:1355: :lists.mapfoldl/3
(elixir) expanding macro: Kernel.@/1
Thanks!
1 Like
NobbZ
2
You can learn about the memory consumption of various erlang base types in the erlang documentation, chapter “advanced”.
<< 0xFFD8::bytes-size(2) >>
doesn’t work, because the integer literal already specifies a unit of 1
, you can’t override it.
I don’t understand why it allows overriding the unit of 1 (byte?) with 16 bits, but not 2 bytes.
Thanks.
NobbZ
4
Integer literals imply a unit(1)
, the unit
can’t be overwritten once set. Also you can’t change the size
once its set.
unit
specifies the number of bits to use for a single “fragment”, size
specifies the number of “fragments”.
grych
5
I think what confuses you is bytes-size
. It is just an alias for binary-size
. If you want to specify the integer size with bytes instead of bits, use:
<< 0xFFD8::size(2)-unit(8) >>
4 Likes
NobbZ
6
Hey cool, I didn’t know that you can specify a new unit
even on literals. Thank you!
1 Like
Thank you very much to you two @NobbZ and @grych.