11.7.2 Signed right shift operator >>

2010-05-17

Unlike <<, this operator actually fills the left bits by the value of the sign (0 for positive, 1 for negative).

ShiftExpression : ShiftExpression << AdditiveExpression

Code: (Meta Ecma)
function evaluate(ShiftExpression << AdditiveExpression) {
var lref = evaluate(ShiftExpression);
var lval = GetValue(lref);
var rref = evaluate(AdditiveExpression);
var rval = GetValue(rref);
var lnum = ToInt32(lval);
var rnum = ToUint32(rval);
var shiftCount = rnum & 0x1F;
return lnum >> shiftCount; // always returns a 32bit signed integer
}

Note that for this right shift, the most significant bit (in this case the sign) is copied to the most significant bit of the result and shifted right. This means that for positive numbers, zero is shifted in from the left. For negative numbers, a one is shifted in from the left. This is why -1>>1==-1 (-1 is all ones in binary). More examples of binary shift operations in 11.7.

This operator can be used to mimic the ToInt32() operation: n>>0