Javascript bitwise-and assignment operator (&=) Introduction _ Basics

Source: Internet
Author: User
Tags bitwise

The Javascript bitwise-and assignment operator (&=) Sets the result of a bitwise AND operation on the value of a variable and an expression value. Variables and expressions are treated as 32-bit binary values, and the general expressions are all decimal integers, which need to be converted to the corresponding binary, and then 0 forward to complement 32 bits.

Copy Code code as follows:

Result &= "Integer 2"
Equivalent to
Result = result & "Integer 2"

& performs a bitwise AND operation on each digit of two 32-bit expressions. If the two digits are 1, the result is 1. Otherwise, the result is 0.

bit 1 bit 2 bit 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 &= bitwise AND assignment operators:

Copy Code code as follows:

92 is 1001 and complements 32 bits 00000000000000000000000000001001
var expr1 = 9;

5 is 00000000000000000000000000000101.
var expr2 = 5;

/*
00000000000000000000000000001001
&
00000000000000000000000000000101
=
00000000000000000000000000000001
=
1
*/

var result = expr1 & expr2;

alert (result);
Pop-up "1"

Expr1 &= expr2;
alert (EXPR1);
Pop-up "1"

JavaScript assignment operators and expressions


The JavaScript assignment operator is responsible for assigning values to variables, and the JavaScript assignment operators include =,+=,-=,*=,/=,%=
A JavaScript assignment expression that is concatenated with an assignment operator and an Operation object (operand) that conforms to the rule's JavaScript syntax.

JavaScript assignment operators vs. Assignment expression syntax

var i+=a;

+ =--Assignment operator
The meaning of the above expression is: The value of I plus a, given the variable i.

JavaScript assignment operators vs. assignment expressions

-= th> Td>i*=5
operator = + *= /= % =
name assignment operator add assignment operator subtraction assignment operator multiplication assignment operator Division assignment operator modulus assignment operators (remainder assignment operator)
expression i=6 i+=5 i-=5 i/=5 i%=5
example var i=6; i+=5; i-=5; i*=5; i/=5; i%=5;
I results 6 one 1 1.2 1
equivalent to   i=i+5; i=i-5; i=i*5; I=I/5; i=i%5;

Example explanation

There is an essential difference between the ex-self-increment operator and the post-self-increment operator, their similarities are all added by 1 for themselves, the difference being that the first increment operator is preceded by 1, then the operand is used, and then the increment operator is preceded by the value of the operand, plus 1. For example:

Copy Code code as follows:

var A;
var i=6;

(ex-Gaga) I plus 1, I equals 7, and I give a, and a equals 7.
A=++i;
document.write (i);
document.write (a);

i=6;
(after Gaga) assigns the I value to a, so a equals 6, and the last I plus 1,i equals 7.
a=i++;
document.write (i);
document.write (a);



Results:

Copy Code code as follows:

7
7
7
6

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.