Basic type algorithm question Learning (EPI)

Source: Internet
Author: User

1. How to quickly obtain the parity bit of a 64-bit number in the parity check method. (If the number of digits in 1 is odd, the parity bit is 1. If the number of digits in 1 is even, the parity bit is 0)

A. the brute-force enumeration method uses one digit for calculation and one digit for one shift, which is less efficient.

 

B. x = x & (x-1), each time the first digit of 1 is invalid, count the number of 1.

 

C. You can use the look-up table method. For example, 8 bits are a unit, and 255 of the space corresponds to the number of 1 s. The number of 1 s is counted by unit.

2. Change the I-bit and J-bit of the 64-bit number.

If a. I bit is the same as J bit, there is no need to replace it. If it is different, the replacement method is (1 <I | 1 <j) ^ The original number.

3. Sort all the bits of a 64-bit number in reverse order.

A. constantly call the methods changed above.

 

B. Use the look-up table method, in units.

4. Not applicable to multiplication, division, modulo operation to obtain the maximum common divisor of two numbers (GCD)

A. subtraction can be used to simulate the moving phase division.

B. recursion keeps narrowing down the problem scale.

5. returns the prime number before 1 to n.

A. Create a table between 1 and N, and then use a number multiple to keep the table from being a prime number to false. If one digit is used to determine whether it is a prime number, the efficiency is too low.

6. Use the bitwise operation to complete the multiplication of two unsigned numbers

A. the binary system uses bitwise operations to simulate addition operations.

7. Perform Division operations using addition, subtraction, multiplication, And bits.

A. The general idea is subtraction, but 2 ^ K can be subtracted, which can speed up the subtraction.

Basic type algorithm question Learning (EPI)

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.