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 are the skills of bit operation

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

Share

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

This article mainly introduces "what are the skills of bit operation". In daily operation, I believe that many people have doubts about their in-place operation skills. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the skills of bit operation?" Next, please follow the editor to study!

Cognitive bit operation

What is a bit operation?

All the numbers in the program are stored in binary form in the computer memory. Bit operation is to directly operate on the binary bits of integers in memory.

Bit operation is the direct operation of binary numbers, so what kinds of bit operations are there?

Common operators are and (&), or (|), XOR (^), reverse (~), and left shift (signed right shift > > unsigned right shift). Let's take a closer look at the rules of each bit operation.

Bit operation & (and)

Rule: logical AND operation for binary corresponding bits (the result is 1 only when the values of corresponding bits are 1, otherwise it is 0), that is, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.

For example: 2 &-2

Bit operation | (or)

Rule: logic or operation is performed between binary corresponding bits (if one of the corresponding bits is 1), that is, 0 | 0 # 0 0 | 1 # 1 | 1 # 1

For example: 2 |-2

Bit operation ^ (XOR)

Rule: binary corresponding bits are operated by logical XOR (XOR) (when the value of the corresponding bits is 1, otherwise it is 0), that is, 0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 1 = 0

For example: 2 ^-2

Bit by bit reverse ~

Rule: binary 0 becomes 1BI 1 becomes 0.

Shift operator

Left shift operation: after moving to the right, the leftmost value of the left complement (may be 0, may be 1)

Shift operation to the right >

For the left shift operator > and the left complement primitive bit > >, if it is a positive number, there is no dispute that the left side is a complement 0, which achieves the effect of dividing by 2; if it is a negative number, the positive and negative value of the left complement 0 > will change from a negative number to a relatively large positive number. If it is the original bit on the left (negative complement 1) > > then the whole number is still negative, which is equivalent to the effect of dividing by 2.

The following figure can help you understand the shift operator of negative numbers:

At this point, I think you should have a preliminary understanding of bit operation. Compare some of the cases mentioned above here so that you may understand it more clearly:

Tips on bit operation

Here are some common bit operation tips.

Judge odd and even numbers

When judging odd and even numbers normally, we would write:

If (n% 2 = = 1)

/ n is an odd number

}

Using bit operations, you can write:

If (n & 1 = = 1) {

/ n is an odd number.

}

Its core is to determine whether the last bit of the binary is 1, if it is 1, then the result plus 2 ^ 0 = 1 must be an odd number, otherwise it is an even number.

Exchange two numbers

For the traditional exchange of two numbers, we need to use a variable to assist in completing the operation, which might be like this:

Int team = a

A = b

B = team

However, using bit operations can complete the numerical exchange without the need for extra space:

A = a ^ b; / / a = a ^ b

B = a ^ b; / / b = (a ^ b) ^ b = a ^ 0 = a

A = a ^ b; / / a = (a ^ b) ^ (a ^ b ^ b) = 0 ^ b = 0

The principle has been written in the notes, does it feel very diao?

Binary enumeration

When dealing with subset problems, we sometimes use binary enumerations to traverse various states (more efficient than dfs backtracking). This belongs to the problem of permutation and combination, which is the two states of use and non-use for each item (position), which can be expressed as 1 and 0 in the binary system. In the implementation, it can be calculated and solved by enumerating the number range to analyze the characteristics of each symbol bit of each binary number.

The code implementation of binary enumeration is as follows:

For (int I = 0; I < (1)

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