Bitwise JavaScript Operators

G Paul
4 min readMay 21, 2020

--

According to Wikipedia, Bitwise Operators are similar to logical operators except that they work on the bit level rather than decimal, hexadecimal, or octal numbers. Additionally, Bit-level programming only deals with 0 and 1. Moreover, they are used in numerical computations to make the calculation process faster.

Like C, C++, Java, Python, and various other languages, JavaScript also supports bit-wise operations. In JavaScript, a number is stored as a 64-bit floating-point number but the bit-wise operation is performed on a 32-bit binary number i.e. to perform a bit-operation JavaScript converts the number into a 32-bit binary number (signed) and perform the operation and convert back the result to a 64-bit number.

JavaScript supports the following bitwise operators:

Now let’s understand the above mentioned Bitwise operators in detail:

· Bitwise AND (& ): This operator performs ‘AND’ operation on each bit of its operands. E.g., in the below code snippet, it is equal to the calculation where firstly, convert the values of 2 and 5 in binary format. Secondly, do AND (&) of each bit. Finally, convert the binary result to the decimal result. As none of the bits on the same position is 1, the final result is 0 for & operation of all the bits individually.

var a = 2;// on bit level 2 = 0010

var b = 5;// on bit level 5 = 0101

var c = a & b; //the value of c will be 0, since a & b is 0000 when performed an operation on bit a level

· Bitwise OR ( | ): This operator performs ‘OR’ operation on each bit of its operands. E.g., in the below code snippet, it is equal to the calculation where firstly, convert the values of 2 and 5 in binary format. Secondly, do OR(|) of each bit. Lastly, convert the binary result to the decimal result. The binary result will be 0111, which converted to decimal will be equivalent to 7.

var a = 2; // on bit-level 2 = 0010

var b = 5; // on bit-level 5 = 0101

var c = a | b; //the value of c will be 7 since a | b is 0111 when performed OR operation on a bit level

· Bitwise XOR ( ^ ): This operator performs ‘XOR’ operation on each bit of its operands. The XOR means exclusive OR operation. Here, the result will be true only when either of the operands is true, but not both.

var a = 2;// on bit-level 2 = 0010

var b = 7; // on bit-level 7 = 0111

var c = a ^ b; //the value of c will be 5, since a^ b is 0101 when performed XOR operation on a bit level

· Bitwise NOT ( ~ ): This operator will invert all the bits on the operand.

var a = 3;// on bit-level 2 = 0011

var c = ~b; //the value of c will be -4 since a & b is 1100 where last bit will represent a positive or negative value

Bitwise shift operators

The bitwise shift operators include two operands: the first is a quantity to be shifted, and the second specifies the number of bit positions by which the first operand is to be shifted. The direction of the shift operation is controlled by the operator used.

Shift operators convert their operands to 32-bit integers in big-endian order and return a result of the same type as the left operand. We will use only the low five bits of the right operand.

· Left Shift (<<): This operator moves all bits to the left side by as many numbers of places as mentioned in the operand. In addition to this, new bits fill with zero value. In other words, this operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

var a = 3; // 3 = 0011

var b = a <<1; the value of c will be 6, since a<<1 is 0110

· Right Shift (>>): This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left. Since the new leftmost bit has the same value as the previous leftmost bit, the sign bit (the leftmost bit) does not change. Hence the name “sign-propagating”.

This operator will move all the bits to the right side by as many numbers of places as mentioned in the operand. In addition to this, new bits fill with zero value.

var a = 3; // 3 = 0011

var b = a >>1; the value of c will be 1, since a<<1 is 0001

Conclusively, that’s all about BitWise Operators.

--

--