http://elixir-lang.github.io/getting-started/binaries-strings-and-char-lists.htmlhtml
https://elixirforum.com/t/bitstring-and-binary/2351/4git
iex>?a 97 iex>?吳 21556
a的code point是97,也就是110 0001;吳的code point是21556,也就是101 0100 0011 0100。這是unicode標準規定的。github
可是計算機中,咱們是以byte爲單位來存放的,那麼110 0001只須要一個字節,填滿8位就是0110 0001;101 0100 0011 0100須要兩個字節,填滿16位就是0101 0100 0011 0100。這個過程就是所謂的utf-8編碼過程。bash
A bitstring is a type that stores arbitrary number of bits, you can have a 5bit bitstring whereas binary stores arbitrary number of bytesthis
Here is some code that should make things clearer:編碼
# bitstring bs = << 3 :: size(2) >> # => 2 bits 11 IO.inspect bs # => <<3::size(2)>> IO.inspect is_bitstring(bs) # => true IO.inspect is_binary(bs) # => false # binary bin = << 3 >> # => 8 bits or 1 byte IO.inspect bin # => <<3>> IO.inspect is_bitstring(bin) # => true IO.inspect is_binary(bin) # => true
A binary is just a collection of bytes, so it has to have a number of bits that is divisible by 8 (i.e. a byte). So you can have a 8 bit binary, 16 bit binary and so on. If your binary is not divisible by 8, e.g. 7bits, 15bits, 14 bits, 23bits, you have a bitstring. And since a bitstring can have any number of bits even a binary is a bitstring. However, the inverse is not true.spa
So a subset of bitstrings are binaries, and a subset of binaries are strings. Like this:code