char * str = "Whoa";
char ch = *++str;
The prefix ++ operator and the unary * operator have the same precedence, but they associate right-to-left. So, again, str and not *str is incremented. Because the ++ operator is in prefix form, first str is incremented, and then the resulting pointer is dereferenced. Thus, str moves to point to the h character, and the h character is assigned to ch.
Note that Table D.1 uses
Appendix B, “C++ Reserved words,” lists alternative representations for some of the operators.
E. Other Operators
To avoid terminal obesity, the main text of this book doesn’t cover three groups of operators. The first group consists of the bitwise operators, which let you manipulate individual bits in a value; these operators were inherited from C. The second group consists of two-member dereferencing operators; they are C++ additions. The third group includes operators added by C++11: alignof and noexcept. This appendix briefly summarizes these operators.
Bitwise Operators
The bitwise operators operate on the bits of integer values. For example, the left-shift operator moves bits to the left, and the bitwise negation operator turns each 1 to a 0 and each 0 to a 1. Altogether, C++ has six such operators: <<, >>, ~, &, |, and ^.
The Shift Operators
The left-shift operator has the following syntax:
Here
13 << 3
The vacated places are filled with zeros, and bits shifted past the end are discarded (see Figure E.1).
Figure E.1. The left-shift operator.
Because each bit position represents a value twice that of the bit to the right (see Appendix A, “Number Bases”), shifting one bit position is equivalent to multiplying the value by 2. Similarly, shifting two bit positions is equivalent to multiplying by 22, and shifting n positions is equivalent to multiplying by 2n. Thus, the value of 13 << 3 is 13×23, or 104.
The left-shift operator provides a capability often found in assembly languages. However, an assembly language left-shift operator directly alters the contents of a register, whereas the C++ left-shift operator produces a new value without altering existing values. For example, consider the following:
int x = 20;
int y = x << 3;
This code doesn’t change the value of x. The expression x << 3 uses the value of x to produce a new value, much as x + 3 produces a new value without altering x.
If you want to use the left-shift operator to change the value of a variable, you must also use assignment. You can use regular assignment or the <<= operator, which combines shifting with assignment:
x = x << 4; // regular assignment
y <<= 2; // shift and assign
The right-shift operator (>>), as you might expect, shifts bits to the right. It has the following syntax:
Here
17 >> 2
For unsigned integers, the vacated places are filled with zeros, and bits shifted past the end are discarded. For signed integers, vacated places may be filled with zeros or else with the value of the original leftmost bit. The choice depends on the C++ implementation. (Figure E.2 shows an example that illustrates filling with zeros.)
Figure E.2. The right-shift operator.
The right-shift operator. Shifting one place to the right is equivalent to integer division by 2. In general, shifting n places to the right is equivalent to integer division by 2n.
C++ also defines a right-shift-and-assign operator that you can use to replace the value of a variable by the shifted value:
int q = 43;
q >>= 2; // replace 43 by 43 >> 2, or 10
On some systems, using left- and right-shift operators may produce faster integer multiplication and division by 2 than using the division operator, but as compilers get better at optimizing code, such differences are fading.
The Logical Bitwise Operators
The logical bitwise operators are analogous to the regular logical operators, except they apply to a value on a bit-by-bit basis rather than to the whole. For example, consider the regular negation operator (!) and the bitwise negation (or complement) operator (~). The ! operator converts a true (or nonzero) value to false and a false value to true. The ~ operator converts each individual bit to its opposite (1 to 0 and 0 to 1). For example, consider the unsigned char value of 3:
unsigned char x = 3;