Home Web Front-end JS Tutorial Introduction to the use of Javascript bitwise AND assignment operator (&=)_Basic knowledge

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

May 16, 2016 pm 05:01 PM
assignment operator

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does '+=' mean in python What does '+=' mean in python Jan 05, 2023 pm 05:53 PM

In Python, "+=" refers to the "addition assignment" operator, which is a type of assignment operator. Its function is to perform an addition operation first, and then assign the result to the variable on the left side of the operator; the syntax is "x += y", the equivalent form is "x = x + y". The "+=" operator can only assign values ​​to existing variables, because the variable itself needs to participate in the operation during the assignment process. If the variable is not defined in advance, its value is unknown and cannot participate in the operation.

Python Operators: The Ultimate Guide from Newbie to Master Python Operators: The Ultimate Guide from Newbie to Master Mar 11, 2024 am 09:13 AM

Introduction to python operators Operators are special symbols or keywords used to perform operations between two or more operands. Python provides a variety of operators covering a wide range of uses, from basic mathematical operations to complex data manipulation. Mathematical operators Mathematical operators are used to perform common mathematical operations. They include: operator operation examples + addition a + b - subtraction a-b * multiplication a * b / division a / b % modulo operation (take the remainder) a % b ** power operation a ** b // integer division (discard the remainder) a//b Logical Operators Logical operators are used to concatenate Boolean values ​​and evaluate conditions. They include: operator operations examples and logical and aandbor logical or aorbnot logical not nota comparison operations

The Secret Garden of Operators: Discover Hidden Treasures in Python The Secret Garden of Operators: Discover Hidden Treasures in Python Mar 11, 2024 am 09:13 AM

The Secret Garden of Operators Python operators are symbols or keywords used to perform various operations. They enable developers to express complex logic concisely and clearly and improve code efficiency. Python provides a wide range of operator types, each with its specific purpose and usage. Logical Operators Logical operators are used to combine Boolean values ​​and perform logical operations. The main ones are: and: Returns the Boolean value True, if all operands are True, otherwise it returns False. or: Returns a Boolean value True if any operand is True, otherwise returns False. not: Negate the Boolean value, change True to False, and change False to True. Demo code: x=Truey

Uncovering the Power of Python Operators: Writing Elegant and Efficient Code Uncovering the Power of Python Operators: Writing Elegant and Efficient Code Mar 11, 2024 am 09:28 AM

Python operators are a key component of the programming language, enabling developers to perform a wide range of operations, from simple arithmetic to complex bit manipulation. Mastering the syntax, semantics, and functionality of operators is essential to using Python effectively. Arithmetic Operators Arithmetic operators are used to perform basic arithmetic operations. They include addition (+), subtraction (-), multiplication (*), division (/), modulo (%), exponentiation (**), and floor division (//). The following example demonstrates the use of arithmetic operators: >>a=10>>b=5#Addition c=a+bprint(c)#Output: 15#Subtraction c=a-bprint(c)#Output: 5#Multiplication c=a*bprint(c)#output

In C/C++, what is the meaning of operator c=a+++b? In C/C++, what is the meaning of operator c=a+++b? Sep 01, 2023 pm 04:29 PM

Let us consider that in C or C++, there is a similar statement: c=a+++b; So what is the meaning of this line of code? Okay, let a and b be 2 and 5 respectively. This expression can be viewed as two different types. c=(a++)+bc=a+(++b) has post-increment operator and pre-increment operator. How they are used depends on how they are used. There are two basic concepts. Priority and associativity. Now if we check the expression from left to right, the result will be these two. c=(a++)+b→2+5=7c=a+(++b)→2+6=8 Now let’s check which option is selected by the compiler - example code #include<io

Secrets of Python Operators: Mastering the Cornerstone of Programming Secrets of Python Operators: Mastering the Cornerstone of Programming Mar 11, 2024 am 09:19 AM

Python operators are special symbols or words used to perform specific operations on values ​​or to combine values. They are the fundamental building blocks of programming languages ​​and are key to understanding and writing efficient code. Arithmetic Operators Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and remainder. The following are the most commonly used arithmetic operators: +Addition-Subtraction*Multiplication/Division%Remainder Example: x=10y=5print(x+y)#Output: 15print(x-y)#Output: 5print(x*y)#Output :50print(x/y)#Output: 2.0print(x%y)#Output: 0 Comparison Operator The comparison operator is used to compare two values ​​and return a Boolean value (True

Explain the concepts of logical operators and assignment operators in C language Explain the concepts of logical operators and assignment operators in C language Sep 13, 2023 pm 06:17 PM

First, let's learn about logical operators. Logical Operators These are used to logically combine two (or more) expressions. They are logical AND (&&), logical OR (||) and logical NOT (!) logical AND (&&) exp1exp2exp1&&exp2TTTTFFFTFFFF logical OR (||) exp1exp2exp1||exp2TTTTFTFTTFFF logical NOT (!) exp!expTTFT Operator Description Example a= 10,b=20,c=30 output&&logical AND(a>b)&&(a<c)(10>

What does php module equal mean? What does php module equal mean? Feb 02, 2023 pm 07:27 PM

PHP modular equals refers to "%=", which is an extended assignment operator, which means to perform the modulo operation first, and then assign the result to the variable on the left side of the operator; the syntax is "x %= y", the equivalent form is "x = x % y". The extended assignment operator combines = with other operators (including arithmetic operators, bitwise operators and logical operators) to expand it into a more powerful assignment operator; the expanded assignment operator will make the assignment expression Writing is more elegant and convenient.

See all articles