An introduction to the binary number system can be found here.
A Broad Review of All Bitwise Operators in JS
||AND||If both bits are 1 – the result is 1. If at least one of them is 0 – the result is 0.|
||OR||If both bits are 0 – the result is 0. If at least one of them is 1 – the result is 1.|
||XOR||If both bits are the same – the result is 0. If the bits are different – the result is 1.|
||NOT||Inverts all bits in a number.|
||Left shift||Shifts all bits to the left, fills empty slots with zeros.|
||Signed right shift||Shifts all bits to the right, fills empty slots with the leftmost bit, to maintain the sign of the number.|
||Unsigned right shift||Shifts all bits to the right, fills empty slots with zeros.|
Examples and Usage
I suggest starting with a piece of paper and a pencil and writing down all the numbers in binary.
The AND, OR, XOR Operators
^ require two numbers to function properly. They compare the bits in these numbers one-by-one, applying the rules from the table above.
Let’s try them out. The binary representation of a decimal number is in the comment:
It’s easier if you write the binary numbers one under the other like this:
AND 0101 OR 0101 XOR 0101 0110 0110 0110 ---- ---- ---- 0100 0111 0011
Now, we can add
console.log and see if our calculations were right:
An important trick with XOR is that if you place the same number on both sides of it, you’ll always get zero.
On the contrary, if you replace
^ with either
| the number won’t change.
The NOT Operator
~ operator is a different beast. It’s called a unary operator as it applies to a single operand (number).
NOT 0101 => 1010 NOT 0110 => 1001
0, the number is positive, if it’s set to
1 it’s negative.
If you apply the
~ operator the number twice, you’ll get back to where you started:
Bitwise Shift Operators
The bitwise shift requires two numbers to run. The first one is the actual number that’s being shifted and the second is the number of binary digits that should be moved.
Shifting the binary number one digit to the left is the same as making it two times bigger. Unless it’s already so big that there are no bits to hold the result of such a multiplication.
The right shift, contrary to the left shift, makes the number smaller. If the number is large enough and no significant bits will “fall off,” it will divide the number by two. In our case, with the small numbers, it’s not so easy:
Don’t be lazy, check the binary calculation yourself!
Unsigned Right Shift
The unsigned right shift is different from the regular right shift, as it doesn’t maintain the sign of the number. When applied to negative numbers it will always turn them positive as the leftmost bit will become
There were no surprises with
100 and it expectedly got
4 times smaller.
But do you understand why
-100 turned into
Some interviewers, though, like this topic a lot and might ask you to implement some tasks using only bitwise operations. For example, determine if the number is even.
Download it on iOS or Android.
Credit: Source link