Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the operator |= do in JavaScript?

I found the following code in a JS project:

var a = new Array(); a[0] = 0; for (var b = 0; b < 10; b++) {   a[0] |= b;  } 

What does the |= do in the body of the for loop?

The code example is dubious, but has been presented here by V8 for an example of improved performance.

Updated Example

The above example is equivalent to var a = [15]; for most intents and purposes. A more realistic example for the |= operator would be to set up binary flags in a single variable, for example on a permission object:

//Set up permission masks var PERMISSION_1_MASK = parseInt('0001',2); var PERMISSION_2_MASK = parseInt('0010',2); ..  //Set up permissions userPermissions = 0; userPermissions |= hasPermissionOne && PERMISSION_1_MASK; userPermissions |= hasPermissionTwo && PERMISSION_2_MASK; ..  //Use permissions if(userPermissions & PERMISSION_1_MASK){     ..//Do stuff only allowed by permission 1 } 
like image 201
ContentiousMaximus Avatar asked Oct 12 '12 07:10

ContentiousMaximus


People also ask

What does |= mean in Arduino?

Description. The compound bitwise OR operator |= is often used with a variable and a constant to "set" (set to 1) particular bits in a variable.

What does |= mean in Ruby?

Ruby has an or-equals operator that allows a value to be assigned to a variable if and only if that variable evaluates to either nil or false .

What does |= mean in C++?

|= just assigns the bitwise OR of a variable with another to the one on the LHS.


2 Answers

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

like image 127
Jacob George Avatar answered Sep 26 '22 11:09

Jacob George


x |= y; 

is equivalent to

x = x | y; 

where | stands for bitwise OR.

like image 28
freakish Avatar answered Sep 26 '22 11:09

freakish