Skip to content
Search

C++:BitwiseOperators

lastmod 2018-11-13created 2015-08-284 edits

Shift Operators

The bitwise shift operators are the right shift operator (shift_expression), which moves the bits of >> to the right, and the left shift operator (shift_expression), which moves the bits of << to the left.

The following is the description of the shift operators from the C++ ISO specification (INCITS/ISO/IEC 14882-2011[2012]), sections 5.8.2 and 5.8.3.

The value of E1 << E2 is E1 left-shifted E2 bit positions. Vacated bits are zero-filled. If E1 has an unsigned type, the value of the result is E1 × 2^E2, reduced modulo one more than the maximum value representable in the result type. Otherwise, if E1 has a signed type and a non-negative value, and E1 × 2^E2 is representable in the corresponding unsigned type of the result type, then that value, converted to the result type, is the resulting value. Otherwise, the behavior is undefined.

The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type, or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2^E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.

See also

author Zer0 sitemap