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

How to perform bit-by-bit inversion in javascript

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how to reverse javascript by bit". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to reverse bit by bit in javascript".

In javascript, you can use the "~" operator to achieve bitwise inversion, and the syntax "~ Operand"; the "~" operator can convert a specified Operand into a binary integer and invert the binary Operand bit by bit.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, bitwise inversion can be achieved using the "~" operator. For example, ~ 1 =-2, false color, etc.

The "~" operator can view the binary representation of the specified value and reverse the binary Operand bit by bit

Step 1: convert the Operand to a 32-bit binary integer.

Step 2: carry out the inversion operation bit by bit.

Step 3: convert binary inverse codes to decimal floating point numbers.

If a bit non-operation is performed on 12, the return value is-13.

Console.log (~ 12); / / return value-13

The following figure parses the process of bit non-operation of 12 in the form of an expression.

How to reverse the calculation by bit

Operation rule steps of bitwise inversion:

1. Convert the decimal system to the original code

Convert to binary original code, the highest bit is the symbol bit, 0 is positive, 1 is negative

Decimal-> original code 1-> 00000001-1-> 10000001 2, convert the original code to the inverse code

The inverse code of a positive number is the original code, while the inverse code of a negative number is that the symbol bits remain unchanged, and the rest bits are inverted.

Decimal-> original code-> inverse code 1-> 00000001-> 00000001-1-> 10000001-> 11111110 3, reverse code converted to complementary code

The complement of a positive number is still the original code, and the complement of a negative number is to add 1 to the inverse code.

Decimal system-> source code-> inverse-> complement 1-> 00000001-> 00000001-> 00000001-1-> 10000001-> 11111110-- > 11111111 4, complement takes back the original code

After the positive integer complement is reversed, the symbol position is 1, which is a negative integer, so the original code is obtained by reverse operation according to the way that the negative integer calculates the complement.

Inverse operation to get the original code, first convert the inverted complement to the inverse code, the formula: inverse code = complement-1, and then convert the inverse code into the original code, the symbol bits remain unchanged, and other bits are inverted.

Decimal-> original-> inverse-> complement-> complement to reverse-> convert from anti-complement to inverse-> convert to original 1-> 00000001-> 0000001-> 00000001-> 11111110-- > 11111101-> 10000010

After the negative integer complement is reversed, the symbol position is 0, which is a positive integer, because the inverse code and complement of the positive integer are themselves, so there is no need for inverse operation.

Decimal-> original-> inverse-> complement-> complement to get the original-- 1-> 10000001-> 11111110-> 11111111-> 00000000 5, Convert the original code to binary decimal-> original-> inverse-> complement-> complement to reverse-> convert anti-complement to reverse-> convert to original-> convert to binary 1-> 00000001-> 0000001-> 00000001-> 11111110-> 11111101-> 10000010-> 2 decimal-> original code-> inverse code-> complement-> convert to binary-1-> 10000001-> 11111110-- -> 11111111-> 00000000-> 0

So, ~ 1, 2, 2, 2, 1, 2, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4

The function of reversing and reversing

Operator ~ means bitwise inversion. On the surface, it doesn't make sense. In fact, floating-point numbers can be turned into integers in JS.

Console.log (~ 1.11); / 1 console.log (~-25.11); / /-25 are all the contents of the article "how to reverse bit by bit in javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Development

Wechat

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

12
Report