In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how the seven bit operators in JavaScript are used in actual combat. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Bit operator
Operator is used for the underlying operation of a numeric value, that is, to manipulate the bits (bits) that represent the data in memory.
All values in ECMAScript are stored in IEEE 754 64-bit format, but bit operations are not applied directly to 64-bit representations, but instead, values are converted to 32-bit integers, followed by bit operations, and then the results are converted to 64-bit.
For developers, it is as if there are only 32-bit integers, because the storage format for 64-bit integers is not visible. Now that you know this, you only need to consider 32-bit integers.
Signed integers use the first 31 bits of 32 bits to represent integer values. The 32th bit represents a numeric symbol, such as 0 for positive and 1 for negative. This bit is called a symbolic bit (sign bit), and its value determines the format of the rest of the value. Positive values are stored in a true binary format, where each of the 31 bits represents the power of 2. The first bit (called bit 0) represents 20, the second bit represents 21, and so on.
If a bit is empty, it is filled with 0, which is equivalent to ignoring it. For example, the binary format of the number 18 is 000000000000000000000000000000010010, or a more compact 10010. The latter is the five significant bits used, which determines the actual value (as shown in the following figure).
Bit by bit non ~
The bitwise non-operator is represented by the wavy character (~), and its function is to return a complement to the value. Bitwise non-is one of the few binary mathematical operators in ECMAScript. Look at the following example:
Let num1 = 25; / / binary 00000000000000000000000000011001let num2 = ~ num1; / / binary 11111111111111111111111111100110console.log (num2); / /-26
Here, the bitwise non-operator acts on the number 25, and the result is 26. From this, you can see that the ultimate effect of bitwise non is to reverse and subtract 1 from the value, just like the result of doing the following:
Let num1 = 25 setlet num2 =-num1-1 console.log (num2); / / "- 26"
In fact, although the results returned by both are the same, bit operations are much faster. This is because the bit operation is done on the underlying representation of the value.
Bitwise and &
Bitwise and operators are denoted by the sum sign (&) and have two operands. In essence, bitwise and is to align each bit of two numbers, and then perform corresponding and operations on each bit based on the rules in the truth table.
Bitwise and operation returns 1 when both bits are 1 and 0 when either bit is 0. Let's take a look at an example where we sum the values 25 and 3, as follows:
Let result = 25-3 position console.log (result); / / the bitwise and operational results of 25 and 3 are 1.
Why? Take a look at the following binary calculation process:
As shown in the figure above, in the binary representation of 25 and 3, only the two numbers on bit 0 are 1. So all other bits of the resulting value are filled with 0, so the result is 1.
Bitwise or |
Bitwise or operator is represented by the pipe character (|), and there are also two operands. Bitwise or follow the following truth table:
A bitwise or operation returns 1 when at least one bit is 1 and 0 when both bits are 0. Still using the bitwise and example, if you perform bitwise OR for 25 and 3, the code is as follows:
Let result = 25 | 3position console.log (result); / / 27
It can be seen that the result of bitwise or operation of 25 and 3 is 27:
Of the two numbers involved in the calculation, 4 are 1, so they correspond directly to the results. The binary code 11011 equals 27.
Bitwise XOR ^
Bitwise XOR is represented by a delimited character (^), and there are also two operands. The following is a table of truth values by bit XOR:
The difference between bitwise XOR and bitwise OR is that it returns 1 only when one bit is 1 (if both bits are 1 or 0, 0 is returned). Perform bitwise XOR for values 25 and 3:
Let result = 25 ^ 3 console.log (result); / / 26
It can be seen that the bitwise XOR result of 25 and 3 is 26, as follows:
Both numbers are 1 in 4 digits, but the 0 th bit of both numbers is 1, so that bit becomes 0 in the result. The 1 on the remaining bits does not have a corresponding 1 on another number, so it is passed directly to the result. The binary code 11010 equals 26. (note that this is 1 less than the result of a bitwise or operation on the same two values. )
Move left > 5; / / equals binary 10, that is, decimal 2
Similarly, a vacancy occurs after the shift. However, after moving to the right, the vacancy appears on the left and after the symbol bit (see figure 3-3). ECMAScript fills these spaces with the values of symbolic bits to get the full number.
Move unsigned right >
Unsigned right shift is represented by 3 greater than signs (> >), which shifts all 32 bits of the value to the right. For positive numbers, the unsigned right shift is the same as the signed right shift. Still take the previous example of moving the sign to the right. If 64 moves 5 bits to the right, it becomes 2:
Let oldValue = 64; / / equals binary 1000000 let newValue = oldValue > 5; / / equals binary 10, that is, decimal 2
For negative numbers, sometimes the difference can be very large. Unlike the signed right shift, the unsigned right shift fills the vacancy by 0, regardless of the symbol bit. For positive numbers, this has the same effect as a signed right shift. But for negative numbers, the result is much worse. The unsigned right shift operator treats the binary representation of negative numbers as the binary representation of positive numbers. Because a negative number is a complement to its absolute value, the result becomes very large after moving to the right, as shown in the following example:
Let oldValue =-64; / / equals binary 11111111111111111111111111000000let newValue = oldValue > 5; / / equals decimal 134217726
After moving 5 bits to the right of-64 unsigned, the result is 134 217 726. This is because the binary representation of-64 is 1111111111111111111111111000000, which is shifted unsigned to the right but regards it as a positive value, that is, 4 294 967 232. After moving this value 5 digits to the right, the result is 00000111111111111111111111111111111111111110, that is, 134 217 726.
Ingenious use in actual combat
1. Judge odd and even numbers
/ / even number & 1 = 0max / odd & 1 = 1console.log (2 & 1) / / 0console.log (3 & 1) / / 1
two。 Use ^ to complete the exchange of values
Let a = 2let b = 5a ^ = bb ^ = aa ^ = bconsole.log (a) / / 5console.log (b) / / 2
3. Use ~ to make a judgment
/ / commonly used to judge if (arr.indexOf (item) >-1) {/ / code} / / bit by bit non-1 =-(- 1)-1 take anti-1if (~ arr.indexOf (item)) {/ / code}
4. Use &, > >, | to complete the conversion between RGB values and hexadecimal color values
/ * hexadecimal color value to RGB * @ param {String} hex hexadecimal color string * @ return {String} RGB color string * / function hexToRGB (hex) {var hexx = hex.replace ('#', '0x') var r = hexx > > 16 var g = hexx > > 8 & 0xff var b = hexx & 0xff return `rgb (${r}, ${g}) ${b}) `} / * RGB color to hexadecimal color * @ param {String} rgb RGB binary color string * @ return {String} hexadecimal color string * / function RGBToHex (rgb) {var rgbArr = rgb.split (/ [^\ d] + /) var color = rgbArr [1] > 0) / / 3console.log (3.1415 > > No negative console.log (3) 1415 > > 0) / / 3 the above is how the seven bit operators in JavaScript are used in actual combat. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.