Bitwise Operators in Javascript

Bitwise operators Javascript

Bitwise operators, as the name suggests, are going to help us do some operations on bits. So, we are going to work in bits here, I.e 0 and 1. So, if you are not familiar with bits, and their concepts, don’t worry, because we are just going to see the basics of this, and as of now, we are not going to go deep into this. But you can explore and understand the working of bits, and some other concepts on your own since it is pretty interesting.

Here are some bitwise operators in Javascript that we are looking upon →

Bitwise Operators in Javascript

&This is the bitwise AND operator. This gives 1 if and only if both bits are 1. (you will get this with the example discussed below. the Same thing follows for the other symbols)
|This is the bitwise OR operator. This gives 1 if at least one bit is 1.
^This is the bitwise XOR operator. This gives 1 if the bits are not the same.

There are many other bitwise operators like Bitwise NOT, left shift, right shift, etc.

Now, let’s have a look at a very simple program, through which, we can understand the working of bits.

If we have a look at the console, the output we get is something like this –

Now, as you can see, the bitwise AND operation on 9 and 5 gave 1 as output. Next, the bitwise OR operation between the same numbers has 13 as output, and lastly, the bitwise XOR operation on those numbers gave 12 as output. Now, let’s understand why is this happening, and why are we getting such output.

For understanding this, we need to get the concept of bits. Well, bits are just 0 and 1. So, any number that we are dealing with, needs to be converted to binary(which means these 9 and 5 need to be represented as binary). But how do you represent some numbers in binary form? It’s pretty easy. I will tell you one simple way, but quite an informal way of converting some numbers into binary. Also, we are using this way for here only, since we need to quickly move on to other topics (studying binary digits is huge in itself, so you can explore more about it if required, but here we are going to cover enough).

So, let’s say we want to convert 9 into binary. For this, we are going to add the powers of 2 such that it results in 9. So, starting from 2 to the power 0, we go on adding till we get 9 as the sum. So, it comes out to be something like this (we will be multiplying the powers of 2 by 1, which are going to be considered, and the ones who are not going to be considered, we are going to multiply them with zero so that they are not considered) –

(23 × 1) + (22 × 0) + (21 × 0) + (20 × 1) = 8 + 0 + 0 + 1 = 9.

So, our binary conversion here becomes 1001 for 9.

Similarly, for 5, it comes out to be like this –

(23 × 0) + (22 × 1) + (21 × 0) + (20 × 1) = 0 + 4 + 0 + 1 = 5.

So, our binary conversion here becomes 0101 for 5.

Note that here, if we are going with bigger numbers, we are going to need higher powers, like 4 5 6, etc.

So, you can try some other numbers as well, and convert them into binary numbers. Like, if we consider another number, let’s say 15, then the binary for this would be –

(23 × 1) + (22 × 1) + (21 × 1) + (20 × 1) = 15.

You can give it a try for some other numbers.

So, let’s come back to our example now. Here, we are having numbers 9 and 5. First of all, let’s try the AND operation. As we have seen earlier, we are going to work with bits here. So, we are going to go bit by bit. The AND operation gives 1 as output when both the bits are 1. Otherwise, the output is 0.

Let’s have a look at this table below, to understand the output for different bit combinations that we can have –


So, as you can see, when both bits A and B are 1, the output is also 1, and the output is zero otherwise.

So, for our example, the answer for AND operation between 9 and 5 is as follows –



If you try to convert the output back to decimal, you will observe that the output comes out to be 1.

If you are unfamiliar with how to convert binary numbers to decimal numbers, we have to do something similar to what we have been doing for converting decimal to binary here. The last bit(which is also called the least significant bit), should be multiplied by 20. and then, going to the left, you have to multiply the bits by increasing powers of 2. For example, have a look at how we convert the binary number 0001 to a decimal number.

(23 × 0) + (22 × 0) + (21 × 0) + (20 × 1) = 0 + 0 + 0 + 1 = 1.

Now, for the next operation (OR operation), we can have a look at the below table, which explains to us the bit-by-bit output for all the possible bits, when we are doing the OR operation.


So, as you can see from the table when either of the bits is 1, the output is also 1, and otherwise, the output is zero. So, when we are doing the bitwise OR operation on the given data 9 and 5, we again need to convert it to binary, and the OR operation goes something like this –



Notice that we are going bit by bit for the operation here. Now, if we convert the binary number back to decimal, the output comes out to be 13. Let’s check the conversion for binary number 1101 to decimal number 13. Have a look –

(23 × 1) + (22 × 1) + (21 × 0) + (20 × 1) = 8 + 4 + 0 + 1 = 13.
So, as you can see, we got the output as 13, and above is the explanation of how we got this output.

Now, in the next operation, we are doing XOR operations on 9 and 5. In XOR, we get 1 as an output, when both bits are different. Refer to the below table, and you will get a broad idea about XOR operation –


So, as you can see, when both the bits are different, we are getting the output as 1, and otherwise, we are getting 0 as output. Now, let’s explain how are we getting 12 as an output in the above program. Again for that, we are going to convert the numbers 9 and 5 are to be converted to binary(and we know how the conversion is done)

So, let’s have the calculation now –



As you can see, when we are going bit by bit when the bits are different, then only the output bit is 1, and it is zero otherwise. So, the output comes out to be 1100, which, when converted to decimal, comes out to be 12 (you can give it a try for conversion).

You can explore the concept in more depth, since there are some more bitwise operators in Javascript like left shift, right shift, bitwise NOT operator, etc. You can try them as well. Also, we have understood the concept of binary digits in brief here, and you can explore that too since it is a pretty broad topic in itself.