您的位置:首页 > 其它

c programming language learn notes 9

2006-09-23 20:50 375 查看
终是把位操作给看了的说

有两点新发现,

位操作是对于intger的,就是int,char,unsigned,long类

转化符的操作数只能为,无符号

^_^

另外,一道习题

觉得谭爷爷那本书里面的比老外的要好些,也容易理解

Write a function
rightrot(x,n)
that returns the value of the integer
x
rotated to the right by
n
bit positions.

Greg's Cat 0 solution



unsigned rightrot(unsigned x, unsigned n)
{
while (n > 0) {
if ((x & 1) == 1)
x = (x >> 1) | ~(~0U >> 1);
else
x = (x >> 1);
n--;
}
return x;
}

下面讲讲谭爷爷的方法,

1.b=a<<(16-n);//将a的右端n位先放到b的高n位中

2.c=a>>n;//将a右移n位,其左面高位n位补0

3.c=c|b;//按位或运算
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: