Availability: |
| ||||||||
Property/method value type: | Number primitive | ||||||||
JavaScript syntax: | - | anOperand1 >> anOperand2 | |||||||
Argument list: | anOperand1 | A value to be shifted | |||||||
anOperand2 | A distance to shift the left operand |
This is sometimes called shift right with sign extension.
The bitwise shift right operator converts its left operand to a 32 bit integer and moves it rightwards by the number of bits indicated by the right operand.
As the value is shifted rightwards, bits that roll out of the right end of the register are discarded. The left-hand end of the register containing the sign bit is duplicated to sign-fill the value as it shifts. Shifting rightwards by 32 bits will fill the buffer with all zero or all one bits according to the value of the sign bit at the outset.
Because the value is converted to an integer, any fractional part is discarded as the shift begins.
The right hand operand is converted to a 5-bit value with a bitwise mask to limit the distance of the shift to 32 bits. This can cause unexpected results if the right-hand side is derived from an expression that may yield a value larger than 32.
The associativity is left to right.
Refer to the Operator Precedence topic for details of execution order.
You can accomplish bitwise shift rights by dividing values using powers of 2. Dividing a value by 2 shifts rightwards by one bit position.
<HTML> <HEAD></HEAD> <BODY> <SCRIPT> myValue1 = 0x00FF00; myValue2 = myValue1 >> 4; document.write("Val 1 : " + binary32(myValue1) + "<BR>"); document.write("Result : " + binary32(myValue2) + "<BR>"); // Binary convertor (ignore sign bit on MSIE) function binary32(aValue) { myArray = new Array(32); for(myEnum=0; myEnum<32; myEnum++) { if(aValue & Math.pow(2, myEnum)) { myArray[31-myEnum] = "1"; } else { myArray[31-myEnum] = "0"; } } return myArray.join(""); } </SCRIPT> </BODY> </HTML>
Prev | Home | Next |
Bitwise shift operator | Up | Bitwise shift right and assign (>>=) |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |