最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What do these JavaScript bitwise operators do? - Stack Overflow

programmeradmin7浏览0评论
  • 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?

  • 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?

Share Improve this question edited Sep 22, 2014 at 2:10 tckmn 59.3k27 gold badges117 silver badges156 bronze badges asked Dec 26, 2010 at 20:38 DarkLightADarkLightA 15.7k18 gold badges51 silver badges57 bronze badges 1
  • Also have a look at What are bitwise operators? – Bergi Commented Jul 11, 2016 at 3:53
Add a ment  | 

2 Answers 2

Reset to default 14
<<, >>

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".

Here is an attempt to make things simple for the very beginner.

Prerequisites

You have to be familiar with the binary number system (numbers made of two digits). If you are not then check this link first: https://www.mathsisfun./binary-number-system.html. Just in case the previous link breaks, this answer may help a little: https://stackoverflow./a/32155850/1636522.

Indeed, in order to figure out how these operators work, you need to know which bit sequence is behind the numbers involved in the operation. After that you should be able to understand the following stuffs.

Reminder

Decimal digits and their binary notations:

0    0 | 5  101
1    1 | 6  110
2   10 | 7  111
3   11 | 8 1000
4  100 | 9 1001

What do >>>, >> and << do?

These operators shift a bit sequence to the left or to the right.

 decimal | binary      decimal | binary 
---------|---------   ---------|---------
       9 |    1001           2 |      10
    >> 2 | >>    2        << 2 | <<    2
     = 2 |  =   10         = 8 |  = 1000

What do &, | and ^ do?

These operators bine the bits of two numbers to create a new number.

 decimal | binary     decimal | binary     decimal | binary
---------|--------   ---------|--------   ---------|--------
       5 |    101           5 |    101           5 |    101
     & 6 |  & 110         | 6 |  | 110         ^ 6 |  ^ 110
     = 4 |  = 100         = 7 |  = 111         = 3 |  = 011

How does & work?

For each pair of bits: If at least one of the two bits is 0, the resulting bit is 0. If none of the two bits is 0, the resulting bit is 1.

  101    bit 3 | bit 2 | bit 1
& 110   -------|-------|-------
= 100      1   |   0   |   1
           &   |   &   |   &
           1   |   1   |   0
           =   |   =   |   =
           1   |   0   |   0

How does | work?

For each pair of bits: If at least one of the two bits is 1, the resulting bit is 1. If none of the two bits is 1, the resulting bit is 0.

  101    bit 3 | bit 2 | bit 1
| 110   -------|-------|-------
= 111      1   |   0   |   1
           |   |   |   |   |
           1   |   1   |   0
           =   |   =   |   =
           1   |   1   |   1

How does ^ work?

For each pair of bits: If the two bits are different, the resulting bit is 1. If the two bits are the same, the resulting bit is 0.

  101    bit 3 | bit 2 | bit 1
^ 110   -------|-------|-------
= 011      1   |   0   |   1
           ^   |   ^   |   ^
           1   |   1   |   0
           =   |   =   |   =
           0   |   1   |   1
发布评论

评论列表(0)

  1. 暂无评论