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

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.

主站蜘蛛池模板: 建德市| 交城县| 油尖旺区| 贺州市| 四子王旗| 湘潭市| 开远市| 钟山县| 鹤峰县| 德江县| 马边| 兴业县| 怀柔区| 宁蒗| 锦屏县| 海口市| 新建县| 迁西县| 同仁县| 安康市| 蒙山县| 女性| 汾阳市| 定安县| 巴塘县| 常德市| 连平县| 盐边县| 徐汇区| 巴彦县| 会宁县| 自治县| 涪陵区| 永定县| 双牌县| 嵩明县| 漳州市| 汉寿县| 平利县| 阿尔山市| 鹤壁市|