Skip to content

Commit 56a93fa

Browse files
committed
Improved docs for bitstring.
1 parent a58a4eb commit 56a93fa

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,16 @@ defmodule Kernel.SpecialForms do
201201
202202
We can solve this by explicitly tagging it as a binary:
203203
204-
<<102, rest :: binary>>
204+
iex> rest = "oo"
205+
iex> <<102, rest :: binary>>
206+
"foo"
205207
206208
The type can be integer, float, bitstring/bits, binary/bytes,
207209
utf8, utf16 or utf32, e.g.:
208210
209-
<<102 :: float, rest :: binary>>
211+
iex> rest = "oo"
212+
iex> <<102 :: float, rest :: binary>>
213+
<<64, 89, 128, 0, 0, 0, 0, 0, 111, 111>>
210214
211215
An integer can be any arbitrary precision integer. A float is an
212216
IEEE 754 binary32 or binary64 floating point number. A bitstring
@@ -298,13 +302,20 @@ defmodule Kernel.SpecialForms do
298302
Size and unit can also be specified using a syntax shortcut
299303
when passing integer values:
300304
301-
<< x :: 8 >> == << x :: size(8) >>
302-
<< x :: 8 * 4 >> == << x :: size(8)-unit(4) >>
303-
<< x :: _ * 4 >> == << x :: unit(4) >>
305+
iex> x = 1
306+
iex> << x :: 8 >> == << x :: size(8) >>
307+
true
308+
iex> << x :: 8 * 4 >> == << x :: size(8)-unit(4) >>
309+
true
304310
305311
This syntax reflects the fact the effective size is given by
306312
multiplying the size by the unit.
307313
314+
However, if you specify the unit without specifying the size, you'll get
315+
an `CompileError` message:
316+
317+
<< x :: _ * 4 >> == << x :: unit(4) >>
318+
308319
For floats, `size * unit` must result in 32 or 64, corresponding
309320
to binary32 and binary64, respectively.
310321
"""

0 commit comments

Comments
 (0)