Bitwise Operators in JavaScript

The Bitwise operators in JavaScript work on their operand by acting on them in their binary number (base 2) design form (in special 32-bit numbers form), preferably than in their decimal number (Base 10), octal numbers (Base 8), or hexadecimal number (base 16) system. For example, 1010 is the binary presentation of the decimal number ten. Bitwise processes in JavaScript are made on the operands of the operators in their binary performances, but the output is continuously returned in the usual numerical value form.

Bitwise Operators in JavaScript:)

A bitwise operator in JavaScript changes its operands to the 2’s complement form of their 32-bit signified integer form. Hence, whenever an operator is operated upon an integer, the deriving value is the 2’s supplement form of that integer. The 2’s supplement of an integer is the 1’s supplement of the number (e.g bitwise Not of the number) plus 1.

The provided example is the 32-bit presentation of the number 7

00000000000000000000000000000111

The below is 1′ supplement i.e. ~7

11111111111111111111111111111000

The below is 2’s supplement form which is equal to -7

11111111111111111111111111111001

Bitwise OperatorUsageMeaning
Bitwise ANDa&bIt will turn 1 in each bit position if both the identical bits are 1 otherwise 0.
Bitwise ORa|bIt will return 1 in each bit location if any of the identical bits is 1 otherwise 0.
Bitwise NOT~bIt will flip the bits of the operand a from 1 to 0 and vice versa.
Bitwise XORa^bIt will return 0 in each bit place if both the identical bits are either 1 or 0 elsewise 1 whenever the bits are complex.

Bitwise AND

This is a binary operator indicated by the symbol of “&” which operates an AND operation on the constant pair of identical bits of its arguments. The “&” operator ” would return 1 only if both the bits 1 are else it will return 0. Therefore we can also compare AND operation with increase because both will give the same answer.

ABA&B
000
010
100
111

Example:

10 (base 10) = 00000000000000000000000000001010
13 (base 10) = 00000000000000000000000000001101

Bitwise OR

This is a binary operator indicated by the symbol of a perpendicular bar “|” which shows an OR operation on the constant pair of identical bits of its arguments. The “|” operator would return 1 if either of the bits is 1 or both are 1 else it will return 0. The Bitwise OR “|” is changed from logical operator OR “||” as it works bit by bit.

ABA|B
000
011
101
111

Example

10 (base 10) = 00000000000000000000000000001010
13 (base 10) = 00000000000000000000000000001101

Bitwise NOT

This is a unary operator indicated by the symbol of tiled “~” which makes a NOT control on the identical bits of its argument. The “~” operator would change the bits of the operand e.g. convert 0 to 1 or 1 to 0.

A~A
01
10

Example

10 (base 10) = 00000000000000000000000000001010

Bitwise XOR

This is a binary operator indicated by the symbol of caret “^” which makes an XOR operation on the constant pair of identical bits of its arguments. The “^” operator would return 0 if both of the bits are the same (e.g. both are 1 or both are 0) else it will return 1.

ABA^B
000
011
101
110

Example

10 (base 10) = 00000000000000000000000000001010
13 (base 10) = 00000000000000000000000000001101

Conclusion

The Arguments bitwise operators in javaScript are transformed into 32-bit binary numbers and displayed in the form of bits. Numbers in the arguments following with more than 32 bits get their MSB’s (most significant bit) rejected. The same rule refers to while shifting operation if the bit shift left then the extra bits at MSB are abandoned and during the right shift, the extra bit growing in the rightmost part is rejected.

Before: 110011010100011101001000100000001110010010001.

After:  11101001000100000001110010010001

Each identical bit is joined with each other i.e. the first bit with the first bit of other arguments, the second bit with the second bit, and so on. The operator is being practiced at each bit, hence called bitwise operators in JavaScript.

Practical Application of bitwise operator is Bit flags, communication over socket/ports, compression, encryption, finite state machines, graphics, etc. Bitwise Operators in JavaScript.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top