Abstract

The shift expression is used to shift the bit patterns of an integer by a specified number of poositions. 

Usage

The shift expression is typically used in packing and unpacking bit fields.  Shifting can also be used as a fast multiply or divide by a power of 2 for integers. 

Syntax

Syntax Diagrams

Shift Expression Syntax Diagram

BNF

shift-expression
::= <additive-expression>
::= <shift-expression> <shift-operator> <additive-expression>
shift-operator
::= '>>' | '<<'

EBNF

shift-expression
::= <additive-expression> ( <shift-operator> <additive-expression> ) *
shift-operator

Contextual Constraints

shift-operator
The operands of the shift operators should be of int or unsigned int type. 
The operands of the shift operators will be coerced to int or unsigned int type if possible. 

Semantics

shift-expression
The bit pattern of the left operand is shifted left or right by the number of posistions specified by the right operand.
If the left operand of a shift right is a signed int, the left hand bit positions will be filled with the sign bit (sign extension.)
If the left operand of a shift right is an unsigned int, the left hand bit positions will be filled with zeros.

Reference Links

Shift Operator <shift-operator>
Additive Expression <additive-expression>

Other Languages

C Now
C Shift Expression
C++ Now C++ Shift Expression
Java Now Java Shift Expression
JavaScript Now JavaScript Shift Expression
Pascal Now Pascal Shift Expression
PHP Now PHP Shift Expression