Erlang -- bit syntax

Source: Internet
Author: User

(1) Basic concepts:

1. Binary: Raw binary data

2. the Erlang Virtual Machine optimizes binary input, output, and message transmission, which is very efficient.

3. If the number of digits in binary is a multiple of 8, it is called binary)

4. If the number of digits in the binary is not a multiple of 8, it is called a bitstring)

(2) Basic operations:

1. Binary: it is a column INTEGER (0 ~ 255) or string. Eg: <5, 10, 20>

2. Two important functions in binary operations:

1. term_to_binary (TERM)-> bin. % this function can convert any Erlang data type into a binary type.

2. binary_to_term (BIN)-> Inverse Operation of the term. % term_to_binary Function

3. Bit syntax expression:

<<E1, E2, ...,En>>Ei = Value |     Value:Size |     Value/TypeSpecifierList |     Value:Size/TypeSpecifierList

1. value, which can be any value that can be determined

2. Size, an integer, indicating the bit size occupied by value

3. typespecifierlist (Type-specified list) is a list separated by a hyphen, in the formEnd-sign-type-Unit

1. End: it specifies the byte sequence of the machine.

1. Big, large-end, default, network byte order

2. Little

3. Native is determined by the machine's CPU at runtime,

For example:

{<16 #12345678: 32/big, 16 #12345678: 32/little, 16 #12345678: 32/native, 16 #12345678: 32 >> }. the result is: {<86,120,120, 18,120, 120, >}. note that the hexadecimal format is not 10.

2. Sign: used for pattern matching to mark the data type, because some data is signed:

1. Sign

2. unsigned, default value

3. type indicates the type of the value: Yes

Integer | float | binary | bytes | bitstring | bits | utf8 | UTF16 | UTF32; default value: integer

4. Unit indicates the number of digits occupied by the Unit. Format: Unit: INTEGER:

1. The default unit values of integer, float, and bitstring are 1.

2. Binary, 8

3. No value is required for the utf8, UTF16, and UTF32 types,

For example:

<X: 4/little-signed-integer-unit: 8> % indicates a 32-digit integer that contains a signed integer and uses the binary data represented by a small end.

4. binary matching:

1. Package data:

2. parse data:

For example:

Red = 2 #10000001, Green = 2 #1000000000000010, Blue = 2 #10000100, IO: Format ("before unpack, red is ~ P, green is ~ P, blue is ~ P ~ N ", [red, green, blue]), RGB = <RED: 8/little-signed-integer-unit: 1, Green: 16/little-signed-integer-unit: 1, Blue: 8/little-signed-integer-unit: 1 >>,% RGB is the packaged data IO: format ("The package bin is ~ W ~ N ", [RGB]), <r1: 8/little-signed-integer-unit: 1, G1: 16/little-signed-integer-unit: 1, B1: 8/little-signed-integer-unit: 1 >>= RGB, % R1, G1, B1 is the parsed data IO: Format ("after unpack, red is ~ P, green is ~ P, blue is ~ P ~ N ", [R1, G1, B1]).

Note:

1. Because the default value is unsigned, I use signed here. You can calculate it.


Erlang -- bit syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.