What does the operator |= do in JavaScript?

南笙酒味 提交于 2019-11-27 18:29:11
Jacob George
a[0] |= b

is basically

a[0] = a[0] | b

"|" is an or bitwise operator

Update When a[0] is assigned 0, a[0] in binary is 0000. In the loop,

  1. b = 0

    a[0] = 0 (base 10) = 0000 (base 2)
    b    = 0 (base 10) = 0000 (base 2)
                       ---------------
    a[0] | b           = 0000 (base 2) = 0 (base 10)
    
  2. b = 1

    a[0] = 0 (base 10) = 0000 (base 2)
    b    = 1 (base 10) = 0001 (base 2)
                       ---------------
    a[0] | b           = 0001 (base 2) = 1 (base 10)
    
  3. b = 2

    a[0] = 1 (base 10) = 0001 (base 2)
    b    = 2 (base 10) = 0010 (base 2)
                       ---------------
    a[0] | b           = 0011 (base 2) = 3 (base 10)
    
  4. b = 3

    a[0] = 3 (base 10) = 0011 (base 2)
    b    = 3 (base 10) = 0011 (base 2)
                       ---------------
    a[0] | b           = 0011 (base 2) = 3 (base 10)
    
  5. b = 4

    a[0] = 3 (base 10) = 0011 (base 2)
    b    = 4 (base 10) = 0100 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    
  6. b = 5

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 5 (base 10) = 0101 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    
  7. b = 6

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 6 (base 10) = 0110 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    
  8. b = 7

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 7 (base 10) = 0111 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    
  9. b = 8

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 8 (base 10) = 1000 (base 2)
                       ---------------
    a[0] | b           = 1111 (base 2) = 15 (base 10)
    
  10. b = 9

    a[0] = 15 (base 10) = 1111 (base 2)
    b    =  9 (base 10) = 1001 (base 2)
                        ---------------
    a[0] | b            = 1111 (base 2) = 15 (base 10)
    

At the end of the loop the value of a[0] is 15

x |= y;

is equivalent to

x = x | y;

where | stands for bitwise OR.

As with most assignment operators, it is equivalent to applying the operator using the lefthand value again:

a |= b
a = a | b

Just like

a += b
a = a + b

Look on Moz Dev Net for more.

[Edit: Brain fail, mixed up | and ||. Need more coffee. Modified below]

Since | is the Bitwise OR operator, the result of a|b will be the integer representing the bitstring with all the 1 bits of a and b. Note that javascript has no native int or bitstring types, so it will first cast a and b to int, then do a bitwise OR on the bits. So 9 | 2 in binary is 1001 | 0010 = 1011, which is 11, but 8|2 = 8.

The effect is to add the flag bits of b into a. So if you have some flag WEEVILFLAG=parseInt(00001000,2) :

// a = parseInt(01100001,2)
if(isWeevilish(a))
    a |= WEEVILFLAG;
// now a = parseInt(01101001,2)

will set that bit to 1 in a.

sourabh kasliwal

Returns a one in each bit position for which the corresponding bits of either or both operands are ones.

Code: result = a | b;

^ is the bitwise XOR operator, which returns a one for each position where one (not both) of the corresponding bits of its operands is a one. The next example returns 4 (0100):

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!