Skip to content

Commit b6a70d1

Browse files
committed
Merge pull request #3343 from milmazz/bitstring_doc
Added documentation an examples for bitstring
2 parents ac07b67 + e26c4eb commit b6a70d1

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 11 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,9 +302,11 @@ 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.

0 commit comments

Comments
 (0)