1. Foreword:
Before starting the article, let me give you a few questions for everyone to take a look at:
var num1 = 1 & 0; console.log(num1); // 0 var num2 = 'string' & 1; console.log(num2); // 0 var num3 = true & 1; console.log(num3); // 1 var num4 = undefined | false; console.log(num4); // 0 var num5 = undefined | true; console.log(num5); // 1 var num6 = 23 & 5; console.log(num6); // 5 var num7 = 23 | 5; console.log(num7); // 23
Did everyone do the above questions correctly? We have previously summarized "A Brief Talk on "&&" and "||" in JavaScript". "&&" and "||" are operators in logical operation expressions. So what does an “&” or a “|” mean? What are the characteristics? Next, we will reveal the secrets one by one.
First of all, we must understand that "&" and "|" are bitwise operators.
Bitwise operators are used to operate on values at the most basic level, based on the bits in memory that represent the value. All values in ECMAScript are stored in IEEE-754 64-bit format, but bit operators do not directly operate on 64-bit values. Instead, it first converts the 64-bit value to a 32-bit integer, then performs the operation, and finally converts the result to 64 bits. For developers, since the 64-bit storage format is transparent, the entire process appears as if only 32-bit integers exist.
For signed integers, the first 31 bits of the 32 bits are used to represent the value of the integer. Bit 32 represents the sign of the value: 0 represents a positive number, 1 represents a negative number. This bit representing the sign is called the sign bit, and the value of the sign bit determines the format of the other bits. Where positive numbers are stored in pure binary format, each of the 31 bits represents a power of 2. The first bit (called bit 0) represents 20, the second bit represents 21, and so on. Unused bits are represented by 0 and are ignored. For example, the binary representation of the value 18 is 0000 0000 0000 0000 0000 0000 0001 0010, or more concisely 10010. These are 5 significant bits, and these 5 bits alone determine the actual value.
Negative numbers are also stored in binary code, but the format used is two's complement. To calculate the two's complement of a value, you need to go through the following three steps:
(1) Find the binary code of the absolute value of this value (for example, if you require the two's complement of -18, first find the binary code of 18) ;
(2) Find the binary complement, that is, replace 0 with 1, and replace 1 with 0;
(3) Add 1 to the binary complement obtained.
In this way, the binary representation of -18 is obtained, that is, 1111 1111 1111 1111 1111 1111 1110 1110.
......In ECMAScript, when bit operators are applied to values, the following conversion process occurs in the background: the 64-bit value is converted into a 32-bit value, then the bit operation is performed, and finally Convert the 32-bit result back to a 64-bit value. This way, it appears on the surface that you are operating on a 32-bit value, just as binary operations are performed in a similar manner in other languages. But this conversion process also leads to a serious side effect, that is, when applying bit operations to the special NaN and Infinity values, these two values will be treated as 0.
If bit operators are applied to non-numeric values, the Number() function will be used to convert the value to a numeric value (completed automatically), and then the bit operations will be applied. The result will be a numeric value. ...... (Intercepted from "Javascript Advanced Programming")
2. "&" (bitwise AND):
The bitwise AND operator consists of an ampersand character (&) means that it has two operator numbers. Essentially, the bitwise AND operation is to align each bit of two values and perform an AND operation on two numbers at the same position.
Bitwise AND operation rules: 1 is returned only when the corresponding bits of the two values are both 1. If any bit is 0, the result is 0.
I have talked too much about theoretical things before, but I think theory is very necessary. Next, let’s analyze the examples directly!
Let’s first look at num1, num2, num3 and num6 in the above question. We try to combine the above theory to analyze why the final result is output.
// num1是1和0进行“按位与”操作后的返回值。1的二进制码简写为1,0的二进制码简写为0,根据上面的规则,第二个操作符数为0,结果是0 var num1 = 1 & 0; console.log(num1); // 0 // 第一个操作符数是字符串,按照前言里面的理论,对于非数值的操作符数,先使用Number()函数处理,结果返回NaN,NaN又会被当成0来处理。所以最终结果也是0 var num2 = 'string' & 1; console.log(num2); // 0 // true是布尔类型值,同样使用Number()函数处理,处理后得到数值1,于是表达式就相当于“1 & 1” 进行位运算,当两个数值都为1的时候,结果返回1 var num3 = true & 1; console.log(num3); // 1 // 23的二进制码是:...10111,5的二进制码是:...00101。然后每一位进行对齐处理,结合上面的规则,可以得出10111&00101的结果是:00101。00101就是5 var num6 = 23 & 5; console.log(num6); // 5 // 再加个例子:24的二进制码为...11000,7的二进制码为...00111,相同位置的两个数执行AND操作,结果发现结果是...00000。所以最终结果是0,你算对了吗? var add1 = 24 & 7; console.log(add1); // 0
3. "|" (bitwise OR):
The bitwise OR operator is represented by a vertical bar symbol (|) and also has two operator numbers. Essentially, the bitwise OR operation also aligns each bit of the two values and performs an OR operation on the two numbers at the same position.
Bitwise OR operation rules: 1 is returned as long as one of the corresponding bits of the two values is 1, and 0 is returned only when both bits are 0.
Let’s take the top example and look at it!
// 第一个操作符数为undefined,第二个操作符数是false,均不是数值,所以都要先使用Number()函数处理,处理结果都是返回NaN,NaN又会被当成0处理,于是最终结果是0 var num4 = undefined | false; console.log(num4); // 0 // 第一个操作符数相当于0,第二个操作符数相当于1,结合按位或的规则,最终结果是1 var num5 = undefined | true; console.log(num5); // 1 // 23的二进制码是:...10111,5的二进制码是:...00101。然后每一位进行对齐处理,结合上面的规则,可以得出10111|00101的结果是:10111。10111就是23 var num7 = 23 | 5; console.log(num7); // 23 // 再加个例子:24的二进制码为...11000,7的二进制码为...00111,相同位置的两个数执行AND操作,结果发现结果是...11111。所以最终结果是31,你算对了吗? var add2 = 24 | 7; console.log(add2); // 31
4. Others:
I believe there will be some friends who don’t know how to convert the numerical value into a standard binary code. Is there a quick way? Woolen cloth? The answer is yes.
I randomly found the address of an online conversion tool on the Internet: numerical to decimal conversion (click me to view). (Of course, you can also use other tools you find. In any case, achieving the effect is our ultimate goal)
Finally, I attach the regular chart I summarized in the process of converting binary by handwriting, still You can quickly convert numerical values into binary codes, which is very powerful!
For more detailed explanations of “&” and “|” in Javascript, please pay attention to the PHP Chinese website!

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function