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