Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Js shift operation and its application

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Js shift operation and its application is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

When I look at the source code of some utility functions implemented by lodash, I find that lodash defines some constants of bitMask. I didn't know what the damn thing was at first. I searched it with Google and found that it was something like position operation and computing before. And the source code and the information I searched provided me with another use scenario, and I felt that the application was quite strong, so I might as well summarize it.

What is a shift operation?

First expand the basics you need to know with the following expression.

/ / constant defined in lodash source code

VarCLONE_DEEP_FLAG=1

JavaScript follows the IEEE754 standard, and both integers and decimals are expressed as double-precision floating-point numbers. Double-precision floating-point numbers are 8 bytes, representing 64 bits, so the range of double-precision floating-point numbers is-2 ^ 63 ~ 2 ^ 63-1. However, when performing a bit operation, it is represented by a 32-bit number, that is, 4 bytes, which ranges from `- 2 ^ 31 to 2 ^ 31-1, in which whether it is 32 or 64 bits, the highest bit is a symbolic bit, 0 is a positive number, and 1 is a negative number.

The above expression will be converted to the following when performing a bit operation, and if it exceeds 32 bits, the excess will be omitted.

00000000000000000000000000000001

What are the shift operators

Here are several common operators.

& (bit and)

Leta=1

Bread2

Console.log (aquib) / / 0

/ / 0001

/ / 0010

/ / = 0000

Expand both the variable an and the variable b into 32-bit binary numbers, leaving out the binary representation of 0mema as 0001 and b as 0010, followed by a binary comparison of the corresponding digits, if the same is 1, otherwise it is 0.

| | (bit or) |

Leta=1

Bread2

Console.log (a | b) / / 3

/ / 0001

/ / 0010

/ / = 0011

With reference to the above, the difference is that in the same binary bit, as long as one is 1, the result is 1, so it is 0011.

^ (XOR)

This is a little different from |. The similarity is that if only one number on the same digit is 1, then the result of this digit is 1. The difference is that if the value is the same in the same number of digits, the result is 0.

In a word, the shift operation gives you a brief introduction. I hope you will take a look at it more.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report