Exploration of bit operations and exploration of operations

Source: Internet
Author: User

Exploration of bit operations and exploration of operations
Preface

As the blogger keeps writing articles during the idle time of his work, only the exploration of bit operations here [begin.

PS: Update Time: 2014-10-22.

Some people did not understand the first O (n) operation, so here we will briefly introduce O (n log (n) practices.

I will add the basic practice here, and then write the analysis of the O (n) Practice in more detail. People who understand it may seem cool, sorry.
In addition, I was asked how to solve this problem, assuming that most of them are 4 identical and only one is 2 identical. This problem can be called a 4-2 problem.
In this case, this article mainly explains the problem 3-1.

In order not to deviate from the topic, I wrote a new article dedicated to the n-m issue.

If you do not want to look at the basic practice, you can directly jump to the source of the problem. When you scroll down, you should see a directory and click the source of the problem.


Problem Source

The younger brother sent me a code that was not clearly understood at first glance.

 
 
  1. int run(int n, int* A) {
  2. Int ones = 0; // the flag that appears once.
  3. Int twos = 0; // The second flag is displayed.
  4. for(int i = 0; i < n; i++) {
  5. Ones = (ones ^ A [I]) & ~ Twos; // remove the second appearance. Add the first appearance and the third appearance remain unchanged.
  6. Twos = (twos ^ A [I]) & ~ Ones; // the value of the first occurrence remains unchanged. The value of the second occurrence is added, and the value of the third occurrence is removed.
  7. }
  8. return ones;
  9. }

Then I thought that there may be some rules of bitwise operations, such as allocation rate, combination law, and exchange law.

Awaiting update...

Bit operations are confusing. The c or java median operations have been entangled for a long time and have not been thoroughly studied. Which one can explain this,

About the first program: Generally, the short type occupies 2 bytes in the win32 system, so variable I is converted to binary: 00000000 00000100, written as 60 Bytes: 0x04, it carries out operations with 0xFFFF. we know whether the corresponding bit is 0 or 1, it is the same as the first and later, that is, 0 & 1 = 0; 1 & 1 = 1; therefore, 0x04 & 0 xFFFF = 0x0004; 0x04 is two bytes less than 0xFFFF, and the two bytes less should be two bytes higher than 0x04, the two bytes should be supplemented with the symbol bits of 0x04, And the symbol bits of 0x04 are the first. We can see that the first byte is 0, so the complement of 0x04 is 0x0004, so the output is 4; when I is assigned a value of-4, we know that the negative number is stored as a complement code, so the complement code of-4 is: bitwise inversion, adding one at the end, 11111111 11111110, and then matching 0xFFFF, because there are only two bytes, it must be supplemented to four bytes, and the sign bit is 1, so the complement code is: 0 xFFFE, 0 xFFFE & 0 xFFFF = 0 xFFFE, in this case, the source code is-2, so the second output is-2.

C language, which kind brother and sister can tell me bitwise operations? I cannot understand

Bit operation introduction and practical skills (I): Basics
What is bitwise operation?
All the numbers in the program are stored in binary form in computer memory. The bitwise operation is to directly operate the binary bit of the integer in the memory. For example, the and operation is a logical operator, but the and operation can be performed between integers. For example, if the binary value of 6 is 1011 and the binary value of 11 is, the result of 6 and 11 is 2, which is the result of the logical operation of the binary corresponding bit (0 indicates False, 1 indicates True, empty space is processed as 0 ):
110
& 1011
---------------
0010 --> 2
Bitwise operations directly perform operations on memory data and do not need to be converted to decimal. Therefore, the processing speed is very fast. Of course, some people will say, what is the use of this speed? Computing 6 and 11 has no practical significance. This series of articles will show you what bitwise operations can do, some classic applications, and how to use bitwise operations to optimize your program.
BITs in Pascal and C
The following values of a and B are integers:
C language | Pascal Language
------- + -------------
A & B | a and B
A | B | a or B
A ^ B | a xor B
~ A | not
A <B | a shl B
A> B | a shr B
Note that the logical operation and bitwise operator number in C are different. 520 | 1314 = 1834, but 520 | 1314 = 1, because both 520 and 1314 are equivalent to True in logical operation. The same ,! A and ~ A is also different.
Use of various bitwise operations
= 1. and =
The and operation is usually used for bitwise operations. For example, the result of a number and 1 is the last bit of the binary. This can be used to judge the parity of an integer. the last bit of binary is 0, which indicates that the number is an even number, and the last bit is 1, which indicates that the number is an odd number.
=== 2. or operations ===
The or operation is usually used to assign values unconditionally on Binary location. For example, the result of a number or 1 is to forcibly change the last binary to 1. If you need to change the last bit of the binary to 0, you can subtract one after the number or 1. The actual meaning is to forcibly change the number to the nearest even number.
=== 3. xor operations ===
The xor operation is usually used to perform the inverse operation on a specific binary bit, because the difference or can be defined as follows: 0 and 1 are not the same or 0 is the same, or 1 is the opposite.
The inverse operation of xor is itself. That is to say, the final result of two different or the same number remains unchanged, that is, (a xor B) xor B =. The xor operation can be used for simple encryption. For example, if I want to say 1314520 to my MM, but I am afraid that others will know it, then both parties agree to use my birthday 19880516 as the key. 1314520 xor 19880516 = 20665500, I will tell MM 20665500. MM calculates the value of 20665500 xor 19880516 again and obtains 1314520, so she understands my attempt.
Next let's look at another thing. Define two symbols # And @ (why can't I find the character with a cross in that circle). These two symbols are inverse operations, that is, (x # y) @ y = x. Now, execute the following three commands in sequence. What is the result?
X <-x # y
Y <-x @ y
X <-x @ y
After the first sentence is executed, x becomes x # y. In the second sentence, the essence is y <-x # y @ y. Because the # And @ operations are inverse, then the y becomes the original x. In the third sentence, x is actually assigned (x # y) @ x. If # has an exchange law, then after the value is assigned, x becomes the initial y. The result of these three statements is that the positions of x and y are swapped.
Addition and subtraction are mutually exclusive... other full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.