What is << >> ?
Ulf Wiger
etxuwig@REDACTED
Mon Nov 12 13:38:43 CET 2001
<< and >> are used for the "Bit Syntax", which allows for
constructing binaries and matching binary patterns.
http://www.erlang.org/doc/r8b/doc/extensions/bit_syntax.html
The following example from the documentation illustrates how
the bit syntax can be used in e.g. protocol programming -- in
this case doing pattern matching directly on an incoming IP
datagram.
-define(IP_VERSION, 4).
-define(IP_MIN_HDR_LEN, 5).
DgramSize = size(Dgram),
case Dgram of
<<?IP_VERSION:4, HLen:4, SrvcType:8, TotLen:16,
ID:16, Flgs:3, FragOff:13,
TTL:8, Proto:8, HdrChkSum:16,
SrcIP:32,
DestIP:32, RestDgram/binary>> when HLen >= 5,
4*HLen =< DgramSize ->
OptsLen = 4*(HLen - ?IP_MIN_HDR_LEN),
<<Opts:OptsLen/binary,Data/binary>> = RestDgram,
...
end.
/Uffe
On Mon, 12 Nov 2001, Tanja Godau wrote:
>Hello Erlang Experts,
>
>Could someone please explain to me the purpose of <<>> and how
>of use it?
>
>As seen for example in:
>
>=> io:format(" global code:END (of attribute) from
> ~p~n",[<<?WBXML_END>>]),
> ^^ ^^
>or
>
>=> case catch receive_data(Socket,Timeout,Method,<<>>) of
> ^^^^
>>Thankyou
>
>Tanja
--
Ulf Wiger, Senior Specialist,
/ / / Architecture & Design of Carrier-Class Software
/ / / Strategic Product & System Management
/ / / Ericsson Telecom AB, ATM Multiservice Networks
More information about the erlang-questions
mailing list