C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden C++ for Scientists - Technische Universität Dresden

math.tu.dresden.de
from math.tu.dresden.de More from this publisher
03.12.2012 Views

26 CHAPTER 2. C++ BASICS Operator Action − subtraction, also unary minus + addition ∗ multiplication / division % modulus −− decrement ++ increment The modulus operator yields the remainder of the integer division. The ++ operator adds one to its operand and −− subtracts one. Both can precede or follow the operand. When they precede the operand, the corresponding operation will be performed before using the operand’s value to evaluate the rest of the expression. If the operator follows its operand, C ++ will use the operand’s value before incrementing or decrementing it. Consider the following example: x = 1; y = ++x; x = 1; z = x++; As a result of executing these four lines of code, y will be set to 2, x will be set to 2 and z will be set to 1. The priority and associativity of binary arithmetic operators is the same as we know it from math: multiplication and division precedes addition and subtraction. Thus, x + y ∗ z is evaluated as x + (y ∗ z). Operations of the same priority are left-associative, i.e. x / y ∗ z is equivalent to (x / y) ∗ z. Unary operators have precedence over binary: x ∗ y++ / −z means (x ∗ (y++)) / (−z). Nevertheless, as long as you are still learning C ++ and not entirely sure about the precedences, you might want to add redundant parenthesis instead of wasting hours debugging your program. With these operators we can write our first numeric program: #include int main () { float r1 = 3.5, r2 = 7.3, pi = 3.14159; float area1 = pi ∗ r1∗r1; std::cout ≪ ”A circle of radius ” ≪ r1 ≪ ” has area ” ≪ area1 ≪ ”.” ≪ std::endl; std::cout ≪ ”The average of ” ≪ r1 ≪ ” and ” ≪ r2 ≪ ” is ” ≪ (r1+r2)/2 ≪ ”.” ≪ std::endl; return 0 ; } 2.3.2 Boolean operators Boolean operators are logical and relational operators. Both return boolean values, therefore the name. Operators and their significations are:

2.3. OPERATORS 27 Operator Meaning > greater than >= greater than or equal to < less than = 1 + 7 is evaluated as if it were written 4 >= (1 + 7). Advise Integer values can be treated in C ++ as boolean. For the sake of clarity it is always better to use bool for all logical expression. This is a legacy of C where bool does not exist. Almost all techniques from C work also in C ++— as the language name suggests — but using the new features of C ++ allows you to write programs with better structure. For instance, if you want to store the result of a comparison do not use an integer variable but a bool. bool out of bound = x < min || x > max; 2.3.3 Bitwise operators Bitwise operators allow you to test or change the bits of integers. 5 There are the following operations: Operator Action & AND | OR ˆ exclusive OR ∼ one’s complement (NOT) ≫ shift right ≪ shift left The shift operators bitwise shift the value on their left by the number of bits on their right: • ≪ shifts left and adds zeros at the right end. • ≫ shifts right and adds either 0s, if value is an unsigned type, or extends the top bit (to preserve the sign) if it is a signed type. 5 The bitwise operators work also on bool but it is favorable to use the logical operators from the previous section. Especially the shift operators are rather silly for bool.

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!