Skip to main content

Bitwise Operators

Bitwise operators are used to perform bitwise operations on binary numbers. These operators treat their operands as a sequence of bits and perform operations on each pair of corresponding bits. JavaScript provides several bitwise operators that allow you to perform bitwise AND, OR, XOR, NOT, left shift, and right shift operations.

Unlike logical operators, which work with boolean values, bitwise operators work with binary representations of numbers. They are commonly used in low-level programming, such as hardware interfacing, cryptography, and network programming.

note
  • Bitwise operators are rarely used in everyday JavaScript programming. They are more commonly used in specialized applications that require low-level bit manipulation
  • Bitwise operators convert their operands to 32-bit signed integers before performing the operation

Here are the bitwise operators in JavaScript:

Bitwise AND (&)

The bitwise AND operator (&) performs a bitwise AND operation on each pair of corresponding bits. It returns 1 if both bits are 1, otherwise it returns 0.

Here is the syntax of the bitwise AND operator:

operand1 & operand2

The following truth table shows the result of the bitwise AND operation:

Operand 1Operand 2Result
000
010
100
111

Here is an example of using the bitwise AND operator:

bitwise-and.js
let num1 = 5; // Binary: 101
let num2 = 3; // Binary: 011
let result = num1 & num2; // Binary: 001 (Decimal: 1)
console.log(result); // Output: 1

In the example above, the bitwise AND operator performs calculation on each bit like this:

  101 (5)
& 011 (3)
------
001 (1)
------

Bitwise OR (|)

The bitwise OR operator (|) performs a bitwise OR operation on each pair of corresponding bits. It returns 1 if at least one of the bits is 1, otherwise it returns 0.

Here is the syntax of the bitwise OR operator:

operand1 | operand2

The following truth table shows the result of the bitwise OR operation:

Operand 1Operand 2Result
000
011
101
111

Here is an example of using the bitwise OR operator:

bitwise-or.js
let num1 = 5; // Binary: 101
let num2 = 3; // Binary: 011
let result = num1 | num2; // Binary: 111 (Decimal: 7)
console.log(result); // Output: 7

In the example above, the bitwise OR operator performs calculation on each bit like this:

  101 (5)
| 011 (3)
------
111 (7)
------

Bitwise XOR (^)

The bitwise XOR operator (^) performs a bitwise XOR (exclusive OR) operation on each pair of corresponding bits. It returns 1 if the bits are different, otherwise it returns 0.

Here is the syntax of the bitwise XOR operator:

operand1 ^ operand2

The following truth table shows the result of the bitwise XOR operation:

Operand 1Operand 2Result
000
011
101
110

Here is an example of using the bitwise XOR operator:

bitwise-xor.js
let num1 = 5; // Binary: 101
let num2 = 3; // Binary: 011
let result = num1 ^ num2; // Binary: 110 (Decimal: 6)
console.log(result); // Output: 6

In the example above, the bitwise XOR operator performs calculation on each bit like this:

  101 (5)
^ 011 (3)
------
110 (6)
------

Bitwise NOT (~)

The bitwise NOT operator (~) performs a bitwise NOT operation on each bit. It returns the complement of the operand, which is the inverted value of each bit.

Here is the syntax of the bitwise NOT operator:

~operand

Here is an example of using the bitwise NOT operator:

bitwise-not.js
let num = 5; // Binary: 101
let result = ~num; // Binary: 11111111111111111111111111111010 (Decimal: -6)
console.log(result); // Output: -6

In the example above, the bitwise NOT operator inverts each bit of the operand 5 like this:

  101 (5)
~ 010 (Inverted)
------
11111111111111111111111111111010 (-6)
------

Left Shift (<<)

The left shift operator (<<) shifts the bits of the operand to the left by a specified number of positions. It discards the leftmost bits and fills the rightmost bits with zeros.

Here is the syntax of the left shift operator:

operand << count

Here is an example of using the left shift operator:

left-shift.js
let num = 5; // Binary: 101
let result = num << 1; // Binary: 1010 (Decimal: 10)
console.log(result); // Output: 10

In the example above, the left shift operator shifts the bits of the operand 5 to the left by 1 position like this:

  101 (5)
<< 1
------
1010 (10)
------

Right Shift (>>)

The right shift operator (>>) shifts the bits of the operand to the right by a specified number of positions. It discards the rightmost bits and fills the leftmost bits with the sign bit (the leftmost bit).

Here is the syntax of the right shift operator:

operand >> count

Here is an example of using the right shift operator:

right-shift.js
let num = 5; // Binary: 101
let result = num >> 1; // Binary: 10 (Decimal: 2)
console.log(result); // Output: 2

In the example above, the right shift operator shifts the bits of the operand 5 to the right by 1 position like this:

  101 (5)
>> 1
------
10 (2)
------

That's it! You now know how to use bitwise operators in JavaScript to perform bitwise operations on binary numbers. Happy coding! 🚀

In the next section, we will learn about the operator precedence in JavaScript. Let's keep going! 🚀

Made with ❤️ by Fasakin Henry