570 Part IV: Appendixes
Operator Description Example
<< Bitwise left shift the first operand by the value of the
second operand, zero filling “vacated” bit positions.
var x = 1<<2
//4
>> Bitwise right shift the first operand by the value of the
second operand, sign filling the “vacated” bit positions.
var x = -2>>1
//-1
>>> Bitwise right shift the first operand by the value of the
second operand, zero filling “vacated” bit positions.
var x = -2>>>1
//2147483647
& Bitwise AND var x = 2&3;
//2
| Bitwise OR var x = 2|3;
//3
^ Bitwise XOR (exclusive OR) var x = 2^3;
//1
~ Bitwise negation is a unary operator and takes only one
value. It converts the number to a 32-bit binary number
then inverts 0 bits to 1 and 1 bits to 0 and converts
back.
var x=~1
//-2
TABLE A-13 Binary and Self-Assignment Bitwise Operators
Operator Example
+= var x = 1;
x+= 5;
//6
-= var x = 10;
x -= 5;
//5
*= var x = 2;
x *= 10;
//20
/= var x = 9;
x /= 3;
//3
%= var x = 10;
x %= 3;
//1
<<= var x = 4;
x <<= 2;
//16
>>= var x = 4;
x >>= 2;
//1
TABLE A-14 Binary and Self-Assignment Bitwise Operators