Javascript bitwise AND assignment operator (&=) sets the result of the bitwise AND operation between variable value and expression value. Variables and expressions are regarded as 32-bit binary values, and general expressions contain decimal integers. In this case, they need to be converted into the corresponding binary first, and then 0 is added forward to make up the 32 bits.
& performs a bitwise AND operation on each bit of two 32-bit expressions. If both bits are 1, the result is 1. Otherwise, the result is 0.
位1 | 位2 | 位与 |
---|---|---|
0 | 0 | 0 |
1 | 1 | 1 |
0 | 1 | 0 |
1 | 0 | 0 |
The following example demonstrates how to use the & bitwise AND operator and the &= bitwise AND assignment operator:
// 5 is 00000000000000000000000000000101
var expr2 = 5;
/*
00000000000000000000000000001001
&
000000000000000000000000000000101
=
00000000000000000 00000000000001
=
1
*/
var result = expr1 & expr2 ;
alert(result);
//pop up【1】
expr1 &= expr2;
alert(expr1);
// Pop up【1】
JavaScript assignment operators and expressions
JavaScript assignment operators are responsible for assigning values to variables. JavaScript assignment operators include =, =, -=, *=, / =, %=
An expression that is connected with the assignment operator and the operand (operand) and conforms to the regular JavaScript syntax is called a JavaScript assignment expression.
JavaScript assignment operator and assignment expression syntax
var i =a;
= -- assignment operator
The meaning of the above expression is: change i The value obtained by adding a is assigned to variable i.
JavaScript assignment operators and assignment expressions
Operator | = |
= |
-= |
*= |
/= |
%= |
---|
运算符 | = |
= |
-= |
*= |
/= |
%= |
---|---|---|---|---|---|---|
名称 | 赋值运算符 | 加法赋值运算符 | 减法赋值运算符 | 乘法赋值运算符 | 除法赋值运算符 | 模赋值运算符(求余赋值运算符) |
表达式 | i=6 | i =5 | i-=5 | i*=5 | i/=5 | i%=5 |
示例 | var i=6; | i =5; | i-=5; | i*=5; | i/=5; | i%=5; |
i的结果 | 6 | 11 | 1 | 30 | 1.2 | 1 |
等价于 | i=i 5; | i=i-5; | i=i*5; | i=i/5; | i=i%5; |