There are 7 bitwise operators in JavaScript, namely &, |, ^, ~, >, >>> (C# does not have this operator, but C# can pass > > logical right shift to implement this operation), the bit operations are all performed in binary.
Bitwise AND operator (&)
Returns 1 when the same bits of the two numbers are both 1, otherwise returns 0, for example, 1&2=0, the binary representation of 1 is 0001, the binary representation of 2 is 0010, both The operation returns 0000.
Bitwise OR operator (|)
Returns 1 when two numbers have the same digit but different digits, otherwise returns 0, such as 1|2=3.
Bitwise XOR operator (^)
When two numbers have the same bits and only one is 1, it returns 1, otherwise it returns 0, for example, 1^2=3.
The bitwise NOT operator (~)
~ is a unary operator that negates all bits. Here we must first talk about the storage of negative numbers. Negative numbers are stored in the two's complement of their positive numbers. Therefore, when we perform operations on negative numbers, we must correctly obtain their binary encoding, that is, the two's complement of their positive numbers. code. The complement is realized by inverting and then adding 1. Let’s look at the example below:
First calculate the complement of 3: the binary form of 3 is 00000011, its complement is 11111100, and its complement is 11111101, so the binary code of -3 is 11111101, then we require ~-3, which is to take its complement, which is 00000010. This is the complement of -3, and convert it into decimal 2.
If you try a few more, you will find that the complement of a number is actually its decimal opposite minus 1.
Left shift operator (
The left shift operator collectively shifts all the bits of the number to the left, so that the first bit becomes the second bit, and the second bit becomes the third bit. . . The vacated new bits are filled with 0s. For example, 1
Here we can find that the left shift operation is to multiply the decimal number by the power of 2.
Signed right shift operator (>>)
Since the left shift is multiplied by 2, the right shift must be divided by 2. In fact, this is the case. If the number itself is a positive number, Then add 0 to the high bit, and if it is a negative number, add 1 to the high bit. For example, 3 > The encoding is 1111 1101. Move it 1 bit to the right to get 1111 1110. This is a negative number. The negative number is converted into decimal. First subtract one to get 1111 1101. Invert it to 0000 0010 to get -2.
The signed right shift operation is to divide its decimal number by the power of 2 and discard the remainder.
Unsigned right shift operator (>>>)
The result of the unsigned right shift operation of positive numbers is the same as the signed right shift operation, mainly the unsigned right shift operation of negative numbers. The difference between it and signed right shift is that whether it is a positive number or a negative number, the high bits are supplemented with 0, so for positive numbers, the signed and unsigned operations are the same, but for negative numbers, it is a world of difference. The difference. For example: -1 > 1111 1111 1111 1111 1111 1111 1111, the first digit is 0, which is a positive number. Converting it to decimal is 230+229+...+20=230(1-1/231)/(1-1/2)=231 -1=2147483647, so we finally got the result we need. The result is terrible, use with caution!
Application of bitwise operators:
After talking about it for so long, the ultimate goal is to use these operators. Let’s look at some examples:
RGB value of color and hexadecimal conversion: For example, a color value: # 33cc10, the first two digits represent red (R), the middle two digits represent green (G), and the last two digits represent blue (B). Convert it into binary encoding: 0011 0011 1100 1100 0001 0000 (assigned to color), First we want to get the red value, we need to shift it 16 bits to the right, color>>16, which is 0000 0000 0000 0000 0011 0011. In this way we get R=51, then if we want to get the green value we need to shift it first Shift right by 8 bits, color>>8, to get 0000 0000 0011 0011 1100 1100, and then change the first eight bits to 0, 0000 0000 0011 0011 1100 1100|0000 0000 0000 0000 1111 1111, to get 0000 0 000 0000 0000 1100 1100, In this way we get G=204, and finally take the blue value, which is to simply change the first eight digits to 0, color | 0000 0000 0000 0000 0001 0000, we get B=16, #33cc10 converted to RGB value is (51,204 ,16). On the other hand, converting RGB to hexadecimal is exactly the opposite method, which is G
Determine whether a node is the parent node of another node: For example, there are two nodes a and b. The method of ie is a.contains(b) to determine whether a is a child node of b, while other modern browsers use the method It is a.compareDocumentPosition(b). This return result is not a boolean value. If a and b are the same node, it returns 0. If a and b are in different documents or at least one of them is outside the document, it returns 1. If b is before a, 2 is returned, if a is before b, 4 is returned, if b contains a, 8 is returned, if a contains b, 16 is returned, and 32 is exclusive to browsers. The binary codes of 0, 1, 2, 4, 8, and 16 are respectively 0000 0000, 0000 0001, 0000 0010, 0000 0100, 0000 1000, 0001 0000. We can determine whether a.compareDocumentPosition(b) & 16 is converted into boolean. True or false to determine whether a is a node of b, then why not use a.compareDocumentPosition(b) == 16 to determine? Because a.compareDocumentPosition(b) should return 20 (4+ 16), you can use a.compareDocumentPosition(b) == 20 to operate. The advantage of using the & operator is that we don’t need to consider these, we only need Consider whether the & operation with the value 16 we need can return true. (John Resig has a method to simulate compareDocumentPosition, so that it can also be applied under IE. If you are interested, you can refer to the link at the end of the article~)
Bitwise left shift operation: We know that bitwise left shift by 1 bit is multiplied by 2. Then we can use a
Bitwise right shift: On the one hand, you can use a>>1 instead of a/2. In addition, bitwise right shift can easily convert decimals into integers, such as 3.1415>>0=3, because of the bitwise shift operation The operands must be of integer type (please refer to the ECMA-262 manual for details), so the decimal places will be discarded after the operation~
Note: Bitwise operators require that their numeric operands be of integer type, and these operands are used Represented as a 32-bit integer, the 32nd bit is the sign bit. Moreover, the operands are limited to the 32-bit integer range, and the right operand is required to be between 0 and 31. (The binary encoding in this article is not standardized and is only for convenience~)

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.


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

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

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

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
