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:
-
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments