Bitstrings and Binaries 的不同
来源:互联网 发布:哪里有淘宝买家数据? 编辑:程序博客网 时间:2024/06/10 15:50
Bitstrings and Binaries (Erlang bit syntax minihowto part 2)
What’s the difference between the Binary and Bitstring types in Erlang? Well, abitstring is a sequence of bits of any length; a binary is a sequence of bits where the number of bits is evenly divisible by 8. (So any binary is also a bitstring.)
Some examples:
1> <<2#11110000:5>>.< <16:6>>2> <<2#11110000:6>>.< <48:6>>3> erlang:is_bitstring(<<2#11110000:6>>).true4> erlang:is_binary(<<2#11110000:6>>).false5> erlang:is_bitstring(<<2#11110000:8>>).true6> erlang:is_binary(<<2#11110000:8>>).true
In the Erlang bit syntax there are 2 clarifying (?) type synonyms:
bytes == binarybits == bitstring
which are basically telling you to think of binaries as sequence of bytes, and bitstrings as sequence of bits. Quite obvious right? Well, it wasn’t for me at first.
It’s also worth noting how binaries and bitstrings are converted into text strings. Given the binary<<16#50>>
storing the letter ‘P’,
7> <<16#50>><<"P">>8>erlang:bitstring_to_list(<<16#50>>). #I could've used binary_to_list()"P" %great!9>erlang:bitstring_to_list(<<16#50:5>>).[<<16:5>>] %mmh...
The result of command #9 is not exactly what I had imagined. Yes, it’s a list, but it’s certainly not a text string, and I was expecting a text string. Why was I expecting a text string if the API is namedbitstring_to_list()
? Because if I was in the middle of coding a text manipulation algorithm and calledbitstring_to_list()
, I would probably (wrongly) assume that it would do some magic and transformany bitstring in a usable text string. Why? Because we are used to treat text strings as lists. Why? Because we don’t have APIs that mention “strings”: all the builtin APIs only mention lists. That’s the problem. We are using a list API as if it was a string API, when a generic list is not necessarily a string.
- Bitstrings and Binaries 的不同
- Eclipse C/C++开发环境搭建----Linux版 and linux下的eclipse c编程中的no binaries错误
- Combining multi-architecture binaries with lipo and libtool
- hdu 1153 magic bitstrings
- hdu1153 Magic Bitstrings
- 【HDU】 1153 Magic Bitstrings
- hdu 1153 Magic BitStrings
- Apache 2.2 and 2.4 Windows 32 bits Binaries and Modules php5apache2_4.dll
- Android iOS And WP8 的不同
- ruby and 和 && 是不同的
- ruby and 和 && 是不同的
- eclipse+cdt+cygwin配置的“launch failed no binaries” 错误
- 下载Jmeter经常有Binaries和Source两类包的区别
- The FreeBSD Foundation Announces Java JDK and JRE 1.5 Binaries for FreeBSD
- Placeholder for AMDU binaries and using with ASM 10g (Doc ID 553639.1)
- 不同操作系统下的“回车(CR)” and "换行(LF)"
- break and coutinue在遇到switch的不同
- Using Maven profiles and resource filtering(针对不同环境启用不同的配置值)
- Android开源项目第四篇:开发及测试工具篇
- xcode 5.1.1 无证书真机调试
- Questo ci ' fatto pensare che sarebbe stata una grande storia
- C++中各种数据类型的长度
- Esecuzione dal 1 ° Ottobre on the inside boston
- Bitstrings and Binaries 的不同
- vs2008编译live555源码
- hdu 2955 Robberies
- 国内外信息安全事件(不断更新)
- Open vSwitch详解
- Android Runtime
- SWAP-巧妙扩展你的CPU内存
- 2014网络赛 鞍山赛区
- 黑马程序员--Map集合