"In-depth understanding Computer System" Chapter II Exercise 2_66

Source: Internet
Author: User

Recently intends to read the "in-depth understanding computer system" again, to tell the truth this book is not too many read many times, every read will have a harvest. This is the resolution of the book in the whole time, and I think the more typical, interesting writing city blog, well, this is the origin of this blog post. I implore the gods to shoot bricks.


I. Description of the problem (given the level of translation I cannot bear to look straight at, I have described the problem directly in the book):

Generate mask indicating leftmost 1 in x. assume w = 32.

For example 0XFF00, 0x8000, and 0x6600--and 0x4000.

If x = 0, then return 0.

int leftmost_one (unsigned int x);


Two. The result is correct but not in line with the test instructions solution:

Just beginning to see this problem, think about, suddenly there is a very simple solution, so hurriedly write code tried a bit, well, the results are good, the result is correct. (laughter), but look at the topic carefully, this is not the same as the said!! Paste Code:

int leftmost_one (unsigned int x) {unsigned int base = 0x80000000;while (Base) {if ((X & Base)) Break;base >>= 1;}}

The meaning is very simple, since the topic assumes w=32, then I think, only need to start with the largest number base 32 and X and the operation, if the result is not zero, then this base is x the leftmost 1 of the number represented.


Three. Analysis:

The question is, let's generate a mask to identify the leftmost 1 in X. (in other words, let's find a mask so that x, except for the one with the leftmost 1, the other bit is set to 0), unfortunately the code above does not produce anything called a mask. Here's an analysis of the problem:

Under the title of the book, there is a little hint, "convert the X into a form like 000...111.", well, it is such a subtle hint.

assuming the word length W = 8 at this time, assuming x = 00010110, then the result must be 0001 0000, what kind of mask can achieve this effect, we think of this mask = xxx1 0000, (X can be 0, also can be 1)

So x & mask = 0001 0110 & xxx1 0000 = 0001 0000. The key to the problem now is how to generate this mask from x: (Let's think about that implicit hint)

In X, the leftmost 1 left bit is all 0 or not, so we can use the bitwise operation to put all the bits on the right side of the leftmost 1 to 1 (a good mouthful), as follows:

0001 1111

Then on the basis of the reverse, there are: 1110 0000, and then move right one bit, there are 0111 0000, haha, at this time our mask is generated. Paste Code:

int leftmost_one (unsigned int x) {unsigned int mask = X;mask |= mask >> 1;mask |= mask >> 2;mask |= Mask >&G T 4;mask |= Mask >> 8;mask |= mask >> 16;mask = (~mask) >> 1;return x & Mask;}






"In-depth understanding Computer System" Chapter II Exercise 2_66

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.