Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do these JavaScript bitwise operators do?

  • x <<= y (x = x << y)
  • x >>= y (x = x >> y)
  • x >>>= y (x = x >>> y)
  • x &= y (x = x & y)
  • x ^= y (x = x ^ y)
  • x |= y (x = x | y)

What do these different operators do?

like image 499
DarkLightA Avatar asked Dec 26 '10 20:12

DarkLightA


People also ask

What does the bitwise or operator do?

The | (bitwise inclusive OR) operator compares the values (in binary format) of each operand and yields a value whose bit pattern shows which bits in either of the operands has the value 1 . If both of the bits are 0 , the result of that bit is 0 ; otherwise, the result is 1 .

What is use of Bitwise Operators Explain with examples?

Bitwise operators are usually applied to define flag values in operating systems and driver software. For instance, in a file property, the read-only mode is conceptually expressed as a flag bit in the operating system, and the bitwise operator is used to toggle between the true and the false value.


1 Answers

<<, >>

Bit shift left and right, respectively. If you imagine the left operand as a binary sequence of bits, you are shifting those to the left or right by the number of bits indicated by the right operand.

&, ^, |

These are bitwise and, xor, and or, respectively. You can think of & and | as the counterparts to && and ||, except that they will treat their operands as bit vectors, and perform the logical operations on each of the bits. There is no ^^ operator, but this operation is "xor" or "exclusive or". You can think of "a xor b" as "a or b, but not both".

like image 169
asveikau Avatar answered Oct 26 '22 00:10

asveikau