03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

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.

2.3. OPERATORS 27<br />

Operator Meaning<br />

> greater than<br />

>= greater than or equal to<br />

< less than<br />

= 1 + 7 is evaluated as if it were written 4 >= (1 + 7).<br />

Advise<br />

Integer values can be treated in C ++ as boolean. For the sake of clarity it is<br />

always better to use bool <strong>for</strong> all logical expression.<br />

This is a legacy of C where bool does not exist. Almost all techniques from C work also in<br />

C ++— as the language name suggests — but using the new features of C ++ allows you to write<br />

programs with better structure. For instance, if you want to store the result of a comparison<br />

do not use an integer variable but a bool.<br />

bool out of bound = x < min || x > max;<br />

2.3.3 Bitwise operators<br />

Bitwise operators allow you to test or change the bits of integers. 5 There are the following<br />

operations:<br />

Operator Action<br />

& AND<br />

| OR<br />

ˆ exclusive OR<br />

∼ one’s complement (NOT)<br />

≫ shift right<br />

≪ shift left<br />

The shift operators bitwise shift the value on their left by the number of bits on their right:<br />

• ≪ shifts left and adds zeros at the right end.<br />

• ≫ shifts right and adds either 0s, if value is an unsigned type, or extends the top bit (to<br />

preserve the sign) if it is a signed type.<br />

5 The bitwise operators work also on bool but it is favorable to use the logical operators from the previous<br />

section. Especially the shift operators are rather silly <strong>for</strong> bool.

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

Saved successfully!

Ooh no, something went wrong!