Home Web Front-end JS Tutorial JavaScript binary operation skills analysis_javascript skills

JavaScript binary operation skills analysis_javascript skills

May 16, 2016 pm 05:47 PM
javascript binary

1. Original code, complement code, complement code, positive subtraction to complement addition
js uses 32-bit binary integers when performing binary operations. Since the integers of js are all signed numbers, The highest bit 0 represents a positive number and 1 represents a negative number. Therefore, the integer expression range used in js binary operations is
Copy code Code As follows:

-Math.pow(2,31) ~ Math.pow(2,31)-1 // -2147483648 ~ 2147483647

Original code: highest Bit 0 represents positive, 1 represents negative, and the remaining 31 bits are the binary form of the absolute value of the number (the absolute value of the true value)
one's complement: the one's complement of positive numbers is the same as the original code, and the one's complement of negative numbers is the symbol of the original code bit remains unchanged, and the remaining 31 bits are inverted (0 changes to 1, 1 changes to 0)
Complement code: the complement code of a positive number is the same as the original code, and the complement code of a negative number is the complement code plus 1 (the sign bit participates in the operation, in fact, it only requires Only the complement of -0 involves the carry of the highest bit, so there is no need to worry about - changing due to the sign bit participating in the carry operation when adding 1 to the complement).
The complement of 0: 32 0s, processed as positive numbers, the original code, complement and complement are all 0. The complement code of
-0: the highest bit is 1, and the remaining bits are inverted from the original code of 0, resulting in 32 1s. The complement code of
-0: the complement code is 32 1s plus 1, and the highest bit overflow is discarded. , get 32 ​​0s
Therefore, the complement of positive and negative 0 is 0.
Find the complement of the absolute value of a negative number from its complement: the absolute value of a negative binary number, as long as each bit (including the sign bit) Negate it and add 1 to get its absolute value.
When the computer processes addition and subtraction operations, it uses the complement code to perform operations. Subtraction is regarded as adding a negative number. When processing negative numbers, the correct operation result can be obtained by adding the complement code of the negative number. The complement code is for Born to unify addition and subtraction operations.
The principle of converting positive subtraction to complement addition is 32-bit overflow:
For a 32-bit binary positive integer, its modulus is
Copy code The code is as follows:

Math.pow(2,32) = 4294967296

The maximum expression range of 32-bit positive integers is 4294967296 - 1. When reaching 4294967296, this value must be carried to the 33rd bit. The 33rd bit is the overflow bit and is discarded, only 32 0s are obtained (this principle is the same as the hour hand pointers at 0 o'clock and 12 o'clock on the dial are at the same position. The dial starts with 12 is the modulus), therefore, a number gradually increases, once the number M exceeds 4294967296-1, it can be expressed as MB94967296
and the negative number -M (M is the absolute value) can be expressed as a positive number: 4294967296 - M ( This positive number is the binary positive integer corresponding to the complement of the negative number. The complement of the negative number is a 32-bit binary number, and the sum of its original code is exactly equal to modulo). The principle is the same as the dial. 11 o'clock and negative 1 o'clock point to the same point. a location.
Take -3 as an example:
Copy the code The code is as follows:

(Array( 32).join("0") (3).toString(2)).slice(-32); // The binary number of |-3|, that is, the original code
original code = 00000000000000000000000000000011;
reverse Code = 111111111111111111111111111111100; //The sign bit of the original code is 1, and the remaining bits are inverted
Complement code = 111111111111111111111111111111101; //The inverse code adds 1 because the inverse code is obtained by inverting the lower 31 bits of the original code in positive form, so this The lower 31 bits of the two numbers are all 1, plus the complement sign bit 1, we get 32 ​​1s
Then, there is
complement original code = (complement 1) original code
= (reverse Code original code) 1
= 1 (32 bits are all binary numbers of 1) //Because the complement code is obtained by inverting the lower 31 bits of the original code in positive form and adding the sign bit 1, so these two numbers The lower 31 bits of the sum are all 1, plus the one's complement sign bit 1, we get 32 ​​1's
= Math.pow(2,32)
= 4294967296 //This is exactly the modulus of the 32-bit binary number , the same principle as |-1| 11 = 12

This is: positive number subtraction-> negative number addition-> process of complement addition.
2. Bit operations
Because the integers of js are signed positive numbers by default, only 31 bits can be used in the operation, and developers cannot access the highest bit.
Bitwise operations only occur on integers, so a non-floating point number will be rounded down before participating in bitwise operations.
In order to avoid accessing the sign bit, javascript converts the binary of the negative number into the binary of the sign and its absolute value, such as:
Copy code The code is as follows:

(-123).toString(2) ;// "-1111011"

Bitwise negation (~): unary operation, 1 changes to 0, 0 changes to 1, such as
~123; //-124
You can verify this process: positive numbers are inverted, and the sign bit is Negative, so the result is a negative number. According to Math.pow(2,32) - M can be expressed as -M and can be calculated as follows
Copy code The code is as follows:

parseInt((Array(32).join(0) (123).toString(2)).slice(-32).replace(/d /g,function(v) {
return (v*1 1)%2;
}),2)-Math.pow(2,32) // -124, if it is a negative number, subtract Math.pow (2,32)

It should be noted that javascript bit operations are all signed, so up to 32 bits, the highest bit will be used as the sign bit, and when negated, a positive number should be obtained (take Modulo Math.pow(2,32)'s complement--the addition of two numbers results in modulo, and the two numbers are said to be complements of each other).
The bitwise inversion of an integer M can be calculated as follows:
Copy the code The code is as follows:

((-1*M-1) Math.pow(2,32))%Math.pow(2,32)

Bitwise AND (&): two numbers are the same bits, all are 1, return 1, otherwise return 0
Copy code The code is as follows:

&234 = 106
"00000000000000000000000001111011"
"00000000000000000000000011101010"
-------------------------------- ------------
"00000000000000000000000001101010"

Copy code The code is as follows:

|234 = 251
"0000000000000000000000001111011"
"00000000000000000000000011101010"
-------------------------- --------------------------
"00000000000000000000000011111011"

Bitwise XOR (^): two If the number has the same digit, if one is 1 and the other is 0, then 1 is returned, otherwise 0 is returned
Copy code The code is as follows:

^234 = 145
"0000000000000000000000001111011"
"00000000000000000000000011101010"
------------------------ -----------------------
"00000000000000000000000010010001"

Some features of the XOR operation:
Copy code The code is as follows:

a ^ a = 0 
a ^ b = b ^ a 
a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c
a ^ b ^ a = b
d = a ^ b ^ c It can be deduced that a = d ^ b ^ c //This feature is used in some encryption algorithms
//If the operands are within the appropriate range (no overflow), such as a=1, b=2, exchange the values ​​of the two variables
a = [b ,b=a][0]
//or
a = a^b
b = a^b
a= a^b

using bits The XOR operation uses one number to record multiple pieces of information:
There are several status values ​​​​1, 2, 8, 16....
The rule of these values ​​​​is that only one of their binary digits is 1 , the rest are all 0. Therefore, the result of the bitwise XOR operation of any of them will not have a situation where a certain bit of the two numbers is 1, and the value of the operation is uniquely determined, and That is, if you know the result of the operation, you will know which combination of numbers it is, so that you can use one number to record multiple pieces of information.
Copy code The code is as follows:

00000001
00000010
00000100
00001000
00010000

1^2^4 = 7 // "00000111"
So, if we know that the result is 7 , we know that they are a combination of 1 , 2 , and 4 .
If we want to set a parameter to contain several status values, we can use bitwise OR operation.
For such an example, you can refer to several constants about image types in PHP and PHP error level definitions Several constants.
Such an example may be described with decimal numbers, for example: a single-digit number represents the status of a certain attribute, and a tens-digit number represents the status of another attribute. In this case, each status can have 10 values, there are many combinations that can be described with just one number.
Left shift (<<): All binary bits of a number are shifted to the left, the sign bit remains unchanged, the high bit overflows and is discarded, and the low bit is filled with 0
If there is no overflow, the effect of left shift is to multiply 2.
Right shift (>>): All binary bits of a number are shifted to the right, the sign bit is unchanged, the high bits are filled with 0, and the low bits are discarded.
The effect of the right shift operation is to divide by 2 and move downward Round up.
Signed right shift (>>>): When shifting, the sign bit follows the movement, and the sign bit is also treated as a numerical value. Therefore, the result of this operation is a 32-bit unsigned integer, so the signed right of a negative number The shift will produce a positive integer. A signed right shift of a positive number is the same as an unsigned right shift. This is the only operation that can operate on the sign bit.
-123>>>1 ;//2147483586
Some things to note:
Bitwise operations must be integers. If the operand is not an available integer, it will be 0 As operand
Copy code The code is as follows:

~NaN; // Will execute ~0 , the result is -1
~'x'; // -1
'hello'|0; // 0
({})|0; //0

The displacement operation cannot move more than 31 bits. If you try to move more than 31 bits, take the number of bits modulo 32 and then shift
123>>32 //The actual value is 123>>0 (322 = 0)
123>>33 //actually 123>>1
The expression range of 32-bit signed integer is -Math.pow(2,31) ~ Math.pow(2,31)-1, which is -2147483648 ~2147483647, and the precision of js numbers is double precision, 64 bits. If an integer exceeding 2147483647 is involved in bit operations, you need to pay attention to its binary overflow. After truncating 32 bits, if the 32nd bit is 1, it will be interpreted as Negative (complement).
Copy code The code is as follows:

>>0; //-2147483648
>>0; //0
>>0; //-1
>>0; //1
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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to calculate binary arithmetic How to calculate binary arithmetic Jan 19, 2024 pm 04:38 PM

Binary arithmetic is an operation method based on binary numbers. Its basic operations include addition, subtraction, multiplication and division. In addition to basic operations, binary arithmetic also includes logical operations, displacement operations and other operations. Logical operations include AND, OR, NOT and other operations, and displacement operations include left shift and right shift operations. These operations have corresponding rules and operand requirements.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

See all articles