Thursday, 28 February 2013

JavaScript : Assignment Operators Bitwise Operators

Assignment Operators

An assignment operator assigns a value to its left operand based on the value of its right operand. The first operand must be a variable and basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, a = b assigns the value of b to a.

 In addition to the regular assignment operator "=" the other assignment operators are shorthand for standard operations, as shown in the following table.
ShorthandExpressionDescription
a +=ba = a + bAdds 2 numbers and assigns the result to the first.
a -= ba = a - bSubtracts 2 numbers and assigns the result to the first.
a *= ba = a*bMultiplies 2 numbers and assigns the result to the first.
a /=ba = a/bDivides 2 numbers and assigns the result to the first.
a %= ba = a%bComputes the modulus of 2 numbers and assigns the result to the first.
a<<=ba = a<<bPerforms a left shift and assigns the result to the first operand.
a>>=ba = a>>bPerforms a sign-propagating right shift and assigns the result to the first operand.
a>>>=ba = a>>>bPerforms a zero-fill right shift and assigns the result to the first operand.
a&= ba = a&bPerforms a bitwise AND and assigns the result to the first operand.
a^= ba = a^bPerforms a bitwise XOR and assigns the result to the first operand.
a |=ba = a|bPerforms a bitwise OR and assigns the result to the first operand.

Bitwise Operators

Bitwise operators perform an operation on the bitwise (0,1) representation of their arguments, rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number eight has a binary representation of 1000. Bitwise operators do their operations on such binary representation (for example 1000) but they return standard JavaScript numerical values.
Here is a list of JavaScript's bitwise operators.
OperatorUsageDescription
Bitwise ANDa & bReturns a one in each bit position if bits of both left and right operands are ones.
Bitwise ORa | bReturns a one in each bit if bits of either left or right operand is one.
Bitwise XORa ^ bReturns a one in a bit position if bits of one but not both left and right operand are one.
Bitwise NOT~ aFlips the bits of its operand.
Left shifta << bShifts a in binary representation b bits to the left, shifting in zeros from the right.
Sign-propagating right shifta >> bShifts a in binary representation b bits to the right, discarding bits shifted off.
Zero-fill right shifta >>> bShifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.








No comments:

Post a Comment