Programming Tutorials

Bitwise NOT, AND, OR, XOR operators in Javascript

By: Syed Fazal in Javascript Tutorials on 2008-08-15  

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 three-step process:

  1. The operand is converted to a 32-bit number.
  2. The binary form is converted into its one's complement.
  3. The one's complement is converted back to a floating-point 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.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Javascript )

Latest Articles (in Javascript)