Bitwise NOT, AND, OR, XOR operators in Javascript
By: Syed Fazal Emailed: 1676 times Printed: 2158 times
Latest comments

Bitwise NOT
The bitwise NOT is represented by a tilde (~) and is one of just a few ECMAScript operators related to binary mathematics. The bitwise NOT is a threestep process:

The operand is converted to a 32bit number.

The binary form is converted into its one’s complement.

The one’s complement is converted back to a floatingpoint number.
Example:
var iNum1 = 25; //25 is equal to 00000000000000000000000000011001
var iNum2 = ~iNum1; //convert to 111111111111111111111111111100110
alert(iNum2); //outputs “26”
The bitwise NOT essentially negates a number and then subtracts 1 from it, so 25 becomes –26. Really, the same effect can be achieved by doing this:
var iNum1 = 25;
var iNum2 = iNum1 – 1;
alert(iNum2); //outputs “26”
Bitwise AND
The bitwise AND operator is indicated by the ampersand ( & ) and works directly on the binary form of numbers. Essentially, bitwise AND lines up the bits in each number and then, using the following rules, performs an AND operation between the two bits in the same position:
Bit from First Number Bit from Second Number Result
1 1 1
1 0 0
0 1 0
0 0 0
For example, if you wanted to AND the numbers 25 and 3 together, the code looks like this:
var iResult = 25 & 3;
alert(iResult); //outputs “1”
The result of a bitwise AND between 25 and 3 is 1. Why is that? Take a look:
25 = 0000 0000 0000 0000 0000 0000 0001 1001
3 = 0000 0000 0000 0000 0000 0000 0000 0011

AND = 0000 0000 0000 0000 0000 0000 0000 0001
As you can see, only one bit (bit 0) contains a 1 in both 25 and 3. Because of this, every other bit of the resulting number is set to 0, making the result equal to 1.
Bitwise OR
The bitwise OR operator is indicated by the pipe (  ) and also works directly on the binary form of numbers. Essentially, bitwise OR follows these rules when evaluating bits:
Bit from First Number Bit from Second Number Result
1 1 1
1 0 1
0 1 1
0 0 0
Using the same example as for bitwise AND, if you want to OR the numbers 25 and 3 together, the code looks like this:
var iResult = 25  3;
alert(iResult); //outputs “27”
The result of a bitwise OR between 25 and 3 is 27:
25 = 0000 0000 0000 0000 0000 0000 0001 1001
3 = 0000 0000 0000 0000 0000 0000 0000 0011

OR = 0000 0000 0000 0000 0000 0000 0001 1011
As you can see, four bits contain 1 in either number, so these are passed through to the result. The binary code 11011 is equal to 27.
Bitwise XOR
The bitwise XOR operator is indicated by a caret ( ^ ) and, of course, works directly on the binary form of numbers. Bitwise XOR is different from bitwise OR in that it returns 1 only when exactly one bit has a value of 1. Here is the truth table:
Bit from First Number Bit from Second Number Result
1 1 0
1 0 1
0 1 1
0 0 0
To XOR the numbers 25 and 3 together, use the following code:
var iResult = 25 ^ 3;
alert(iResult); //outputs “26”
The result of a bitwise XOR between 25 and 3 is 26:
25 = 0000 0000 0000 0000 0000 0000 0001 1001
2 = 0000 0000 0000 0000 0000 0000 0000 0011

XOR = 0000 0000 0000 0000 0000 0000 0001 1010
As you can see, four bits contain 1 in either number, so these are passed through to the result. The binary code 11010 is equal to 26.
Javascript Home  All Javascript Tutorials  Latest Javascript Tutorials
Sponsored Links 
If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Comments(2)
1.  View Commenti wanted to know the rules how they are used like first which is executed then second ....please reply me .. View Tutorial By: shlok gupta at 20120324 11:44:24 
2.  View CommentNice explanation.But please explain left shift and right shift operators View Tutorial By: Ramesh Kanagaraj at 20130328 14:27:30 
Most Viewed Articles (in Javascript )

Most Emailed Articles (in Javascript)
