The Shift Operators

The left-shift operator has the following syntax:

value << shift

Here value is the integer value to be shifted, and shift is the number of bits to shift. For example, the following shifts all the bits in the value 13 three places to the left:

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.

Image

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:

value >> shift

Here value is the integer value to be shifted, and shift is the number of bits to shift. For example, the following shifts all the bits in the value 17 two places to the right:

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.

Image

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.121.8