17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

24<br />

For division, there are two choices. First, the / operator always yields a float<strong>in</strong>g po<strong>in</strong>t<br />

number, so for example 4/2 yields 2.0, not 2. Integer division (truncat<strong>in</strong>g) is performed by<br />

the div operator, as <strong>in</strong> 7 div 2, yield<strong>in</strong>g 3.<br />

The rema<strong>in</strong>der of an <strong>in</strong>teger division is given by the rem operator, as <strong>in</strong> 15 rem 4,<br />

yield<strong>in</strong>g 3. (This can differ from what a “modulo” operator would yield, if negative numbers<br />

are <strong>in</strong>volved.)<br />

Other float<strong>in</strong>g-po<strong>in</strong>t arithmetic functions are found <strong>in</strong> the st<strong>and</strong>ard library module math;<br />

these are named directly after the correspond<strong>in</strong>g functions <strong>in</strong> the C st<strong>and</strong>ard library, for<br />

example math:sqrt(2).<br />

There are also some additional <strong>in</strong>teger operators for bitwise operations: N bsl K shifts<br />

the <strong>in</strong>teger N K steps to the left, <strong>and</strong> bsr performs a correspond<strong>in</strong>g arithmetic right shift.<br />

The bitwise logic operators are named b<strong>and</strong>, bor, bxor, <strong>and</strong> bnot. For example, X b<strong>and</strong><br />

(bnot Y) would mask away those bits from X that are set <strong>in</strong> Y.<br />

From numerical data, let’s move on to someth<strong>in</strong>g equally primitive: bits <strong>and</strong> bytes.<br />

2.2.2 – B<strong>in</strong>aries <strong>and</strong> bitstr<strong>in</strong>gs<br />

A b<strong>in</strong>ary is a sequence of unsigned 8-bit bytes, used for stor<strong>in</strong>g <strong>and</strong> process<strong>in</strong>g chunks of<br />

data (often data that comes from a file or has been received over some network protocol). A<br />

bitstr<strong>in</strong>g is a generalized b<strong>in</strong>ary whose length <strong>in</strong> bits is not necessarily a multiple of 8; it<br />

could for <strong>in</strong>stance be 12 bits long, consist<strong>in</strong>g of “one <strong>and</strong> a half” bytes.<br />

Arbitrary bitstr<strong>in</strong>gs are a more recent addition to the language, while whole-byte b<strong>in</strong>aries<br />

have been around for many years, but to a programmer there is very little difference on the<br />

surface of th<strong>in</strong>gs, except that you can do some really nifty th<strong>in</strong>gs these days that used to be<br />

impossible before. S<strong>in</strong>ce the syntax is the same, <strong>and</strong> the name “b<strong>in</strong>ary” is so <strong>in</strong>gra<strong>in</strong>ed, you<br />

rarely hear people (<strong>in</strong>clud<strong>in</strong>g us) talk about “bitstr<strong>in</strong>gs” unless they want to make a po<strong>in</strong>t<br />

about the more flexible length.<br />

The basic syntax for a b<strong>in</strong>ary is:<br />

<br />

that is, a comma-separated list of <strong>in</strong>tegers <strong>in</strong> the range 0 to 255, enclosed <strong>in</strong> >. There<br />

must not be any space between the two delimiter characters on either side, as <strong>in</strong> “<

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!