# Bitwise NOT, AND, OR, XOR operators in Javascript

By: Syed Fazal Emailed: 1590 times Printed: 2037 times

 By: rohit kumar - how this program is work By: Kirti - Hi..thx for the hadoop in By: Spijker - I have altered the code a By: ali mohammed - why we use the java in ne By: ali mohammed - why we use the java in ne By: mizhelle - when I exported the data By: raul - no output as well, i'm ge By: Rajesh - thanx very much... By: Suindu De - Suppose we are executing

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

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;

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;

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;

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;

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.

 1 View Comment`i 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 2012-03-24 11:44:24 2 View Comment`Nice explanation.But please explain left shift and right shift operators` View Tutorial          By: Ramesh Kanagaraj at 2013-03-28 14:27:30

Your email(required, will not be shown to the public):

More Tutorials by Syed Fazal
 switch in Javascript break and continue in Javascript do-while, while, for and for-in loops in Javascript The if statement in Javascript Using && in Javascript Using ! and !! in Javascript Bitwise NOT, AND, OR, XOR operators in Javascript String Concatenation using Java

More Tutorials in Javascript
 Use WinSCP to transfer log files remotely using Javascript Verifying user input in JavaScript Pause execution in JavaScript Javascript to display client date and time on webpage Getting Browser's height and width using Javascript Highlighting text on a page using CSS Using revealTrans to do page transitions in Javascript Password protect a web page using Javascript Diabling Right Click option in a browser using Javascript Scrolling message on the status bar using Javascript window.frames[i] in Javascript Form validation using Javascript Using && in Javascript The if statement in Javascript do-while, while, for and for-in loops in Javascript

More Latest News
Most Viewed Articles (in Javascript )
 Typecasting in Javascript Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers Show how many characters remaining in a html text box using javascript List of Keywords and reverved words in Javascript Use WinSCP to transfer log files remotely using Javascript Highlighting text on a page using CSS Diabling Right Click option in a browser using Javascript switch in Javascript call() and apply() methods in Javascript window.frames[i] in Javascript Using revealTrans to do page transitions in Javascript Password protect a web page using Javascript Scrolling message on the status bar using Javascript History and evolution of Javascript Using toString() in JavaScript to convert data types to String
Most Emailed Articles (in Javascript)
 Show how many characters remaining in a html text box using javascript Bitwise NOT, AND, OR, XOR operators in Javascript Click to remove an element from a select html form using javascript List of Keywords and reverved words in Javascript Typecasting in Javascript this keyword sample in Javascript window.frames[i] in Javascript Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers The if statement in Javascript switch in Javascript Math object and Math functions in Javascript Javascript to display client date and time on webpage Using toString() in JavaScript to convert data types to String Using && in Javascript Form validation using Javascript