DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

bitstring in Erlang


bitstring is the bit sequence literals in Erlang. It's powerful and convenient due to the Erlang's telecom background.

Now we have a bit sequence "0110 0101 1000 1111 1010 0010", whose corresponding hex literal is: "0x65 0x8f 0xa2". To express it with Erlang:

1> Bin = <<16#65, 16#8f, 16#a2>>.
<<101,143,162>>

Every segment divided by comma is called a "segment", it's default size is 8.

But we want divide this sequence in this way: "011 0010 1100 011 1110 10 0010", which can be expressed as "<>":

2> Bin2 = <<3:3, 2:4, 16#c:4, 3:3, 16#e:4, 2:2, 2:4>>.
<<101,143,162>>

So that's why

3> <<3, 17, 23:16>>.
<<3,17,0,23>>
4> <<3, 17, 3323:16>>.
<<3,17,12,251>>

The third segment ":16" consists of 16 bits. So it's 8-bit "normal" literals is:

0 * 256 + 23 = 23
12 * 256 + 251 = 3323

That's it.



Published

May 10, 2014

Last Updated

May 10, 2014

Category

Tech

Tags

  • bitstring 1
  • Erlang 15

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor