Bit operation to optimize the computing speed-the computer newspaper has been released

Source: Internet
Author: User

Not afraid of exams

Pcw-chendx@vip.sina.com

Many questions will be prepared for recruitment related to programming. If you are not careful about the questions, the candidates will follow suit. Especially for recent graduates who do not have much work experience, the answers are often too theoretical, it is difficult to satisfy the Examiner. To this end, we have specifically launched this series. By analyzing the real questions, we can make everyone more practical in answering questions, so that the examiner can be satisfied and find a job smoothly.

 

Bitwise operation Optimized computing speed

Javami Studio Chen yuefeng

Bit operations are boring in BASIC language learning. Therefore, many students and friends have not studied in depth or actually applied this basic knowledge. However, in actual project development, bitwise operations are widely used in many fields due to their speed advantages. Therefore, in practical recruitment exams, bitwise operations are common.

Recruitment questions: Please write them out2Of3The fastest way to power.

Answer a: 2x2x2

Answer B: 1 <3

Answer:B

 

Why is the answerB

Answer a is a mathematical method used to calculate 2's 3rd power. It uses decimal operations to achieve the results required by the question. However, the execution efficiency cannot meet the requirements of the question. Why is this method unable to meet the requirements of the question? This will start with the computing mode of the computer.

The computation mode of a computer is based on binary. Therefore, the decimal computation is converted to binary before computation, and the conversion process reduces the running speed. The fastest way to calculate Power 3 of 2 is to use the shift operator in bitwise operations, which is achieved through binary shift. This method can increase the computing speed.

Through this question, the examiner not only checks whether the applicant has a deep understanding of the execution efficiency of the program, but also checks whether the applicant has the knowledge of bit operations. Bitwise operations are very common in program development. Rational Use of bitwise operations can improve the execution efficiency of programs. Therefore, most of the software we use at ordinary times uses bitwise operations. Next, let's take a look at how bitwise operations are applied in actual development.

Bit operations to store data

Bit operations are used to store data, mainly to reduce the memory occupied by the program. Taking int data as an example, if data is stored in decimal format, a 32-bit int variable can only store one value, if data is stored in binary mode (the disadvantage is that only 0 or 1 data is stored), 32 data records can be stored, which greatly saves memory.

For example, if you store data in the last 2nd bits from the right side of an int variable, the code for storing and reading data is as follows:

Int bdata = 0;

// Store value 1

Bdata = bdata | (1 <(2-1 ));

// Store the value 0

Bdata = bdata &(~ (1 <(2-1 ));

// Read data

Int
N = bdata & (1 <(2-1 ));

Comments:In this Code, the data (0 or 1) to be stored is stored in the second-to-last bit of the variable bdata. Therefore, you only need to change the value of the last and second digits, and the value of other digits does not change. Therefore, no matter how many bdata values are stored in 1, you only need to perform a bitwise OR operation with binary data 10 to calculate the second-to-last position 1, bdata and 0xfffffffd are required for bitwise AND operation to achieve the goal of clearing the second and last bits.

Note that some may consider that when the value 0 is stored, the following code is used:

Bdata = bdata | (0 <(2-1 ));

In fact, this is wrong, because 0, no matter how many bits are moved to the left, is still 0. In this way, when bit or operation is performed, 0 and 1 bits or the result will be 1, you cannot set the corresponding bit to 0. Therefore, you need to use the above Code for implementation.

 

Simple encryption of XOR operations

The bitwise computation is fast, and its unique or characteristic (same as or two digits or itself) can be used to encrypt data, the encryption and decryption speed will be very fast. This encryption method is relatively low, but can be used for general encryption and decryption tasks.

For example, if the key is the number 123, the code for encrypting and decrypting a byte array is as follows:

Byte []
B = {1, 2, 3, 5, 6 };

Byte
Key = 123;

// Code encrypted in sequence

For (int
I = 0; I <B. length; I ++ ){

B [I]
= (Byte) (B [I] ^ key); // uses the unique or encrypted

}

// Decrypted code

For (int
I = 0; I <B. length; I ++ ){

B [I]
= (Byte) (B [I] ^ key); // uses the unique or decrypted

}

Comment: In this Code, array B is encrypted with the key. The encryption process is to make each element and key in array B different or, encrypted data can be stored in actual applications or transmitted over the network. The operation during decryption is the same as that during encryption. Although this simple encryption method is not highly confidential, however, the speed of encryption and decryption is indeed commendable.

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.