Bitwise NOT, AND, OR, XOR operators in Javascript
By: Syed Fazal
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:
-
The operand is converted to a 32-bit number.
-
The binary form is converted into its one’s complement.
-
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.
Archived Comments
1. Nice explanation.But please explain left shift and right shift operators
View Tutorial By: Ramesh Kanagaraj at 2013-03-28 14:27:30
2. i wanted to know the rules how they are used like first which is executed then second ....please rep
View Tutorial By: shlok gupta at 2012-03-24 11:44:24
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Use WinSCP to transfer log files remotely using Javascript
Verifying user input 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
Scrolling message on the status bar using Javascript
Diabling Right Click option in a browser using Javascript
Password protect a web page using Javascript
Using revealTrans to do page transitions in Javascript
Form validation using Javascript
window.frames[i] in Javascript
Math object and Math functions in Javascript