您的位置:首页 > 理论基础

深入理解计算机系统第二版课后习题2.66

2013-02-26 15:29 225 查看
Write code to implement the following function:

/*

*Generate mask indicating leftmost 1 in x. Assume w=32.

*For example 0xFF00 -> 0x8000, and 0x6600 -> 0x4000.

* If x = 0, then return 0.

*/

int leftmost_one(unsigned x);


Your function should follow the bit-level integer coding rules(page 120), except that you may assume

that data type int has w=32 bits.

Your code should contain a total of at most 15 arithmetic, bit-wise, and logical operations.

Hint: Fisrt transform x into a bit vector of the form[0...011...1].

int leftmost_one(unsigned x)
{
unsigned tmp;

//构造从原始x最高位往右所有位都为1的无符号数
tmp = x >> 1;
x |= tmp;

tmp = x >> 2;
x |= tmp;

tmp = x >> 4;
x |= tmp;

tmp = x >> 8;
x |= tmp;

tmp = x >> 16;
x |= tmp;

unsigned y,z;
y = x + 1;
z = y;
y >>= 1;
//倘若原始x最高位为31位
z == 0 && (y = 0x8000);

return y;
}


分析:

题目要求:为数A(0101)产生一个掩码M,A&M结果只保留A中最高位的1,即(0100)。所以(0100)就是A的掩码。

原理:若A为(0101 0101),若能得到B(0111 1111),则B+1得到C(1000 0000),然后C右移一位就可得到要求的掩码M(0100 0000)。

那么关键就是得到上述的B,若A为(0101 0101)(假设从右数第n位为最高位1):

A | (A >> 1) ---> B1(011* ****) , 不管*代表0还是1,现在可得到第n,n-1位为1的数B1。

B1 | (B1>>2) --->B2(0111 1***),不管*代表0还是1, 现在可得到第n,n-1,n-2,n-3位为1的数B2.

B2 | (B2>>4) ---->B3(0111 1111) , 此时,可得到B

显然,若A共w位,按上述过程,当Bi | (Bi>>w/2)一定可以得到B

有一个特殊情况,若A的最高位1恰好在w-1位上,得到的B就是(11......11)全1,这时候B+1得到C(00......00)全0,此时就用到

代码中

z == 0 && (y = 0x8000),


即特殊情况下直接返回0x8000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: