官术网_书友最值得收藏!

Binaries

A binary is group of consecutive bytes. You create them by surrounding the byte sequence with << and >>. Here we are creating a two-byte binary:

iex> <<5, 10>>
<<5, 10>>

In the decimal base, a byte can only contain values up to 255 (otherwise it overflows). If we want to store values greater that 255, we need to tell the runtime to use more space to store this binary:

iex> <<5, 256>>
<<5, 0>>
iex> <<5, 256::16>>
<<5, 1, 0>>

As you can see, when we specify the size (16 bits in this case) we can see that the output as an extra byte and the overflow didn't occur. The size doesn't have to be a multiple of 8. In that case, a binary is usually called a bitstring.

Most programmers will not handle data at such a low level, so your use of binaries may not be that frequent. However, they're extremely useful in certain scenarios, such as processing the header of a file to find a magic number and identify the file type, or even when dealing with network packets by hand.

主站蜘蛛池模板: 永丰县| 武川县| 潼南县| 洱源县| 梅州市| 瑞安市| 铜梁县| 宣化县| 遂川县| 长寿区| 清徐县| 廉江市| 河间市| 高安市| 左贡县| 嘉义市| 象山县| 蓬莱市| 安庆市| 嵩明县| 库尔勒市| 太湖县| 常州市| 金寨县| 壶关县| 德惠市| 海兴县| 葵青区| 十堰市| 无为县| 外汇| 黄陵县| 通海县| 蓝山县| 南安市| 察哈| 咸宁市| 利津县| 阿勒泰市| 东兰县| 旌德县|