Bit - Bitwise and Shift binary operator

About

Operator on bit.

See Reference/Operators/Bitwise_Operators

Bitwise binary operator may also be used as assignment operator

Type

  • the bitwise arithmetic operators (~, &, ^, and |)
  • and the shift operators (<<, >>, and >>>).

Arithmetic

The bitwise arithmetic operators

  • ~ (NOT)
  • & (AND)
  • ^ (XOR)
  • and | OR

They are also Boolean operator.

NOT (~)

  • ~, the Bitwise NOT Operator
// ~x is roughly the same as -(x+1)
console.log(~2);    // -(2+1) ==> -3

XOR (^)

The bitwise XOR expression follows the following rules:

0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

Example

console.log(6 ^ 2); //  110 ^ 010 = 100 = 4

OR (|)

The bitwise OR expression follows the following rules:

0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1

Example:

console.log(6 | 2); //  110 ^ 010 = 110 = 6
console.log(8 | 1); //  1000 ^ 0001 = 1001 = 9

AND (&)

0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

Shift

the shift operators (<<, >>, and >>>).

Documentation / Reference


Powered by ComboStrap