Home > Web Front-end > JS Tutorial > body text

Introduction to the use of Javascript bitwise AND assignment operator (&=)_Basic knowledge

WBOY
Release: 2016-05-16 17:01:31
Original
1841 people have browsed it

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.

Copy code The code is as follows:

result &= [integer 2]
equivalent In
result = result & [integer 2]

& 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:

Copy code The code is as follows:

// 9 The binary value is 1001, and the 32-digit complement is 0000000000000000000000000000100 1
var expr1 = 9;

// 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;
Name Assignment operator
Addition assignment operator
Subtraction assignment operator
Multiplication assignment operator
Division assignment operator
Modular assignment operator (remainder assignment operator)
Expression i=6 i =5 i-=5 i*=5 i/=5 i%=5
Example var i=6;
i =5;
i-=5;
i*=5;
i/=5;
i%=5;

i's results 6
11
1
30
1.2
1

Equivalent to
i=i 5; i=i-5;
i=i*5;
i=i/5;
i=i%5;
Example explanationThere is an essential difference between the pre-increment operator and the post-increment operator. The same point is that 1 is added to itself. The difference is that the pre-increment operator first adds 1 and then uses the value of the operand. The post-increment operator first uses the value of the operand and then adds 1. For example:



Copy code

The code is as follows: var a; var i=6; //(Add before adding) After adding 1 to i, i equals 7, and assigns the value of i to a, so a equals 7 a= i; document.write (i); document.write(a); i=6; //(add after) assign the i value to a, so a is equal to 6, and finally i adds 1, i equals 7 a=i; document.write(i); document.write(a); Result: Copy code The code is as follows: 7 7 7 6
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template