More Arithmetic Operators
As demonstrates, an executing program can alter a variable’s value by performing some arithmetic on its current value. A variable may increase by one or decrease by five.
increments x by one, making it one bigger than it was before this statement was executed. C++ has a shorter statement that accomplishes the same effect:
x--; // Same as x = x - 1;
--x; // Same as x = x - 1;
++y; // Same as y = y + 1;
Code Example
#include <iostream>
int main()
{
int x1 = 1, y1 = 10, x2 = 100, y2 = 1000;
std::cout << "x1=" << x1 << ", y1=" << y1 << ", x2=" << x2 << ", y2=" << y2 << '\n';
y1 = x1++;
std::cout << "x1=" << x1 << ", y1=" << y1 << ", x2=" << x2 << ", y2=" << y2 << '\n';
y2 = ++x2;
std::cout << "x1=" << x1 << ", y1=" << y1 << ", x2=" << x2 << ", y2=" << y2 << '\n';
}
//Output
x1=1, y1=10, x2=100, y2=1000
x1=2, y1=1, x2=100, y2=1000
x1=2, y1=1, x2=101, y2=101
If x1 has the value 1 just before the statement y1 = x1++; then immediately after the statement executes x1 is 2 and y1 is 1. If x1 has the value 1 just before the statement y1 = ++x1; then immediately after the statement executes x1 is 2 and y1 is also 2.
As you can see, the pre-increment operator uses the new value of the incremented variable when evaluating the overall expression. In contrast, the post-increment operator uses the original value of the incremented variable when evaluating the overall expression. The pre- and post-decrement operator behaves similarly.
For beginning programmers it is best to avoid using the increment and decrement operators within more complex expressions.
For example, the statement x = x + 5;
can be shorted to x += 5; This statement means “increase x by five.” Any statement of the form
Bitwise Operators
These special operators allow programmers to examine or manipulate the individual bits that make up data values. They are known as the bitwise operators. These operators consist of &, |, ^, ∼ , >>, and <<.
The bitwise and operator, &, takes two integer subexpressions and computes an integer result.
- If bit 0 in both e1 and e2 is 1, then bit 0 in the result is 1; otherwise, bit 0 in the result is 0.
- If bit 1 in both e1 and e2 is 1, then bit 1 in the result is 1; otherwise, bit 1 in the result is 0.
- If bit 2 in both e1 and e2 is 1, then bit 2 in the result is 1; otherwise, bit 2 in the result is 0
- If bit 31 in both e1 and e2 is 1, then bit 31 in the result is 1; otherwise, bit 31 in the result is 0.
For example, the expression 13 & 14 evaluates to 12, since: Bitwise AND
- 13base(10) = 000000000000000000000000000011012
- &
- 14base(10) = 000000000000000000000000000011102
- 12base(10) = 000000000000000000000000000011002
For example, the expression 13 | 14 evaluates to 15, since: Bitwise OR
- 13base(10) = 000000000000000000000000000011012
- |
- 14base(10) = 000000000000000000000000000011102
- 15base(10) = 000000000000000000000000000011112
For example, the expression 13 ^ 14 evaluates to 3, since: Bitwise XOR
- 13base(10) = 000000000000000000000000000011012
- ˆ
- 14base(10) = 000000000000000000000000000011102
- 3base(10) = 000000000000000000000000000000112
For example, the unsigned expression ∼ 13u evaluates to 4,294,967,282, since: Bitwise NOT
- 1310 = 000000000000000000000000000011012 negate ↓
- ~
- −1410 = 111111111111111111111111111100102
#include <iostream>
int main() {
int x, y;
std::cout << "Please enter two integers: ";
std::cin >> x >> y;
std::cout << x << " & " << y << " = " << (x & y) << '\n';
std::cout << x << " | " << y << " = " << (x | y) << '\n';
std::cout << x << " ^ " << y << " = " << (x ^ y) << '\n';
std::cout << "∼" << x << " = " << ∼x << '\n';
std::cout << x << " << " << 2 << " = " << (x << 2) << '\n';
std::cout << x << " >> " << 2 << " = " << (x >> 2) << '\n';
}
The shift operators move all the bits in an integer to the left or right:
- Shift left (<<). The expression x << y, where x and y are integer types, shifts all the bits in x to the left y places. Zeros fill vacated positions. The expression 5 << 2 evaluates to 20, since 510 = 1012 shifted two places to the left yields 101002 = 2010. Observe that x << y is equal to x×2 y .
Shift right (>>). The expression x >> y, where x and y are integer types, shifts all the bits in x to the right y places. What fills the vacated bits on the left depends on whether the integer is signed or unsigned (for example, int vs. unsigned):