Bitwise shift operators

Bitwise shift operators shift the bits in the left-hand operand integer the specified number of bits given in the right-hand operand, in the specified direction. A shift by one bit left multiplies the number by two, a shift one bit to the right divides by 2. In the following a 2-byte integer is bit-shifted:

    unsigned short s1 = 0x0010;     unsigned short s2 = s1 << 8;     std::cout << std::hex << std::showbase;     std::cout << s2 << std::endl;     // 0x1000      s2 = s2 << 3;     std::cout << s2 << std::endl;     // 0x8000

In this example, the s1 variable has the fifth bit set (0x0010 or 16). The s2 variable has this value, shifted left by 8 bits, so the single bit is shifted to the 13th bit, and the bottom 8 bits are all set to 0 (

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.