Introduction to the use of bitwise AND value assignment operators (& =) in Javascript _ basic knowledge

Source: Internet
Author: User
This article mainly introduces information about the bitwise AND value assignment operators (& amp;) in Javascript. If you need them, you can refer to the bitwise AND value assignment operators (& =) in Javascript ), set the bitwise "and" operation result for the variable value and expression value. Both variables and expressions are considered as 32-bit binary values, while general expressions are all decimal integers. In this case, you must first convert them to the corresponding binary values, and then add 0 to the forward to supplement the 32-bit values.

The Code is as follows:


Result & = [integer 2]
Equivalent
Result = result & [integer 2]

& Perform the 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.

Bit 1 Bits 2 Bitwise AND
0 0 0
1 1 1
0 1 0
1 0 0

The following example shows how to use the & bitwise AND operator and the & = bitwise AND value assignment operator:

The Code is as follows:


// 9 the binary value is 1001, and the 32-bit complement value is 00000000000000000000000000001001.
Var expr1 = 9;

// 5 is 00000000000000000000000000000101
Var expr2 = 5;

/*
00000000000000000000000000001001
&
00000000000000000000000000000101
=
00000000000000000000000000000001
=
1
*/

Var result = expr1 & expr2;

Alert (result );
// [1] is displayed]

Expr1 & = expr2;
Alert (expr1 );
// [1] is displayed]

JavaScript assignment operators and expressions


The JavaScript assignment operator is responsible for assigning values to variables. The JavaScript assignment operators include =, + =,-=, * =,/=, % =
The value assignment operator is connected to the operation object (operand). It is a regular JavaScript syntax expression. It is called a JavaScript value assignment expression.

JavaScript assignment operator and assignment expression syntax

Var I + =;

+ = -- Value assignment operator
The expression above indicates that I is assigned to the variable I by adding the value obtained by.

JavaScript value assignment operator and value assignment expression

Operator = += -= *= /= %=
Name Value assignment operator Addition assignment operator Subtraction assignment operator Multiplication and assignment operators Division assignment operator Modulo value assignment operator (remainder value 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 result 6 11 1 30 1.2 1
Equivalent I = I + 5; I = I-5; I = I * 5; I = I/5; I = I % 5;

Example

There are essential differences between the pre-auto-incrementing operators and the post-auto-incrementing operators. Their similarities are that they add 1 to themselves. The difference is that the pre-auto-incrementing operators Add 1 first, then use the value of the operand. Then, the auto-increment operator first uses the value of the operand and Adds 1. For example:

The Code is as follows:


Var;
Var I = 6;

// (Add before) after I add 1, I is equal to 7, and the I value is given to a, so a is equal to 7
A = ++ I;
Document. write (I );
Document. write ();

I = 6;
// (Add after) Assign the I value to a, so a is equal to 6. Finally, I add 1 and I is equal to 7.
A = I ++;
Document. write (I );
Document. write ();



Result:

The Code is as follows:


7
7
7
6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.