Interview Questions about two-digit operations in C Language

Source: Internet
Author: User

1. Reverse a 32-bit number by bit, that is, 32nd to 1st, and 31st to 2nd ............
What algorithms are the most efficient?
Reference http://www.yuanma.org/data/2007/0723/article_2763.htm
Unsignedint bit_reverse (unsignedint N)
{
N
= (N> 1) & 0x55555555) | (n <1) & 0 xaaaaaaaa );
N
= (N> 2) & 0x33333333) | (n <2) & 0 xcccccc );
N
= (N> 4) & 0x0f0f0f) | (n <4) & 0xf0f0f0 );
N
= (N> 8) & 0x00ff00ff) | (n <8) & 0xff00ff00 );
N
= (N> 16) & 0x0000ffff) | (n <16) & 0xffff0000 );
Return
N;
}
The first line of code is the exchange of parity bits. The second act is the exchange of two bits. The second act is the exchange of parity units. The third act is the exchange of four bits; the fourth behavior is an octal unit, where the parity unit is exchanged. the last row is a hexadecimal unit, and the parity unit is exchanged. So far, 32-bit inversion is complete, and the algorithm is complete.
 
The aforementioned parity, 2, 4, 8, and 16 exchange methods can evolve into four forms, which are essentially the same, you can both first move and then take, both first and then move, and one first shift and the other first and then move
 
Inspired by the above program, you can consider 16, 8, 4, 2, parity to achieve the exchange, that is, the entire left and right side of the SWAp, and then deal with the local swap problem,
 
Divide 32-bit into two 16-bit arrows, and there are two ways to achieve semi-Swap:
1. First, change each arrow to the correct direction, and move the entire arrow left or right to 16 places. The problem is how to change each arrow? The problem becomes how to fold and semi-swap the 16-digit number, and then divide it into two eight-digit arrows. The above methods are implemented in sequence, and the last is parity swap, which can form a recursive algorithm; or, consider the idea of 1 in reverse phase, direct parity swaps, 2, 2 swaps, 4, 4 swaps, 8 swaps, 16, 16 swaps, is a method of backward pushing conditions, which corresponds to the method of the original author.
 
2. Switch the positions of the two arrows, that is, 16 and 16, and divide each arrow into two parts. The two groups are 8 and 8, and so on, four groups of 4 and 4 swaps, eight groups of 2 and 2 SWAPs, and 16 groups of parity SWAps, the implementation method is consistent with the thinking method.
 
The second method is easier to think of. It draws on the idea of binary search and divides it by half layer.
 
For an interview, if you soon write the above algorithm, one means that you are a genius, and the other means that you have done this before, then the study is meaningless. It only means that you are fully prepared, it doesn't mean that you have many talents. Of course, there are very few talents. He can give another question right away. If you can solve it quickly, it might mean that you are a genius, however, the probability is too small. Let's be honest. We certainly cannot say that we have done it, nor that we are a genius. What should we do if we want to install cool B?



2. Use the minimum code to calculate the number of 1 in 32-bit int data (no shift method is required). Data is the object to be counted, and I is the result: While (Data! = 0) {Data & = data-1; I ++;} principle: the bitwise of the binary number and the one minus the first one will bring the lowest one to zero! It's really clever.
PS: If you want the fastest speed, I think it should be the look-up table method. I don't know if it is correct. Please correct it!

Related Article

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.