Quantcast
Channel: What are bitwise shift (bit-shift) operators and how do they work? - Stack Overflow
Viewing all articles
Browse latest Browse all 12

Answer by vidy for What are bitwise shift (bit-shift) operators and how do they work?

$
0
0

The bitwise shift operators move the bit values of a binary object. The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted. The result is not an lvalue. Both operands have the same precedence and are left-to-right associative.

Operator     Usage<<           Indicates the bits are to be shifted to the left.>>           Indicates the bits are to be shifted to the right.

Each operand must have an integral or enumeration type. The compiler performs integral promotions on the operands, and then the right operand is converted to type int. The result has the same type as the left operand (after the arithmetic conversions).

The right operand should not have a negative value or a value that is greater than or equal to the width in bits of the expression being shifted. The result of bitwise shifts on such values is unpredictable.

If the right operand has the value 0, the result is the value of the left operand (after the usual arithmetic conversions).

The << operator fills vacated bits with zeros. For example, if left_op has the value 4019, the bit pattern (in 16-bit format) of left_op is:

0000111110110011

The expression left_op << 3 yields:

0111110110011000

The expression left_op >> 3 yields:

0000000111110110

Viewing all articles
Browse latest Browse all 12

Latest Images

Trending Articles





Latest Images