Home JavaScript Page 2 - The Power of Javascript: Operators continued

# The Power of Javascript: Operators continued

In the last article we talked about some of the unary and binary operators available in Javascript, namely the arithmetic operators, the comparison operators and the increment/decrement operators. In this article we continue our discussion of the Javascript operators; we will discuss how computers represent data, and bitwise operators.

Author Info:
Rating:  / 10
July 26, 2005

SEARCH DEVARTICLES

The Power of Javascript: Operators continued - Bitwise Operators
(Page 2 of 5 )

Bitwise operators are used to perform boolean logical operations (which will be explained shortly) on their operands after converting their numerical representation to binary representation (bits). Javascript stores numerical data in 64-bit variables, but when you use bitwise operators, the interpreter converts the operands to 32-bit integer values. Any fractional value will be removed.

If the value is bigger than the range of the 32-bit value, the interpreter fits the value into 32-bits. If one of the operands is not a numerical value (something like "hi"), the expression evaluates to -1. If the value can be converted to a numerical representation (something like "23"), the interpreter automatically converts it. There are seven bitwise operators available. Let's take a look.

 Operator Operator Explanation & The bitwise AND operator performs a boolean AND operation on the bits of its integer operands. As you know, 1 means that the circuit is turned on, which also means true, and 0 means false. A logical boolean operation produces the value true or false based on the value of each bit in both operands. In the case of the bitwise AND operator it produces 1 if both postition matched bits are 1, otherwise it produces 0 -- because it's like saying return true if the bit in the first operand is true AND the bit in the second operand is true. The statement result = 8 & 3; produces the value 0. If we presented it as the binary representation it will be clearer:  00001000 (8 in binary) &         (using Bitwise AND)  00000011 (3 in binary) ---------  00000000 (0 in decimal) 8 is represented in binary as 00001000 (note that I have removed the rest of the 32 bits because the value is small enough to be represented in 1 byte, 8 bits) and 3 is represented as 11 in binary. There are no matched bits that have the value 1, so the product is 00000000. | The Bitwise OR Operator performs an operation that sets the resulting bit to 1 if the first operand's bit is 1 OR if the second operand's bit is 1 OR even if both of them are 1. It's much more like saying set the resulting bit to one if any of the position matched bits are 1, so the statement result = 8 | 3; evaluates to 11. Let's take a look at the binary representation:  00001000 |  00000011 ---------  00001011 The binary data 00001011 means in decimal 8 + 2 + 1 which equals to 11. ^ The bitwise XOR operator (or the exclusive OR operator) is the same as the bitwise OR operator with one difference: it doesn't set the resultant bit to 1 if both bits are 1. It sets the resultant bit to 1 if only one of the operands' position matched bits is 1. If both of the bits have the value 1, the ^ operator puts 0 in the resultant bit. So the statement result = 7 ^ 3; produces the value 4  00000111 ^  00000011 ---------  00000100 the binary value 00000100 equals 4. ~ The bitwise NOT operator is the only unary bitwise operator. It simply changes the state of all the bits in the value; it turns all 1s to 0s and all 0s to 1s. So the statement result = ~4294967295; produces the value 0 because the decimal value 4294967295 is represented in binary as 32 ones. As you know, when using bitwise operators, the interpreter converts the value to 32-bits, and using the bitwise NOT operator turns all the 1s to 0s, which is why we got the value 0 ~ 11111111111111111111111111111111 ----------------------------------   00000000000000000000000000000000 << The left shift operator doesn't set bits. Instead it moves the bits (of the value in the left operand) to the left by the value of the right operand. Take the value 3 for example, when we shift its binary value by 1 as in the statement result = 3 << 1; it produces the value 6. Remember, 3 is represented in the binary system as 00000011, so when we shift it one time to the left, the value becomes 00000110. Note that when we shift the value to the left it puts 0s on the right. >> The right shift operator shifts bits from left to right. The statement result = 255 >> 5; produces the value 7 because 255 is represented in binary as 11111111. When we shift 5 bits, the value becomes 00000111. Shifting from right is like cutting the number of bits and putting 0s on the left. >>> The right shift with zero fill operator is the same as the >> operator, but it doesn't reserve the sign of the value. We have not discussed how we can reserve the sign in the binary representation so we will not talk about this operator now. I will introduce this concept to you later in the series.