这是什么">> = QUOT;操作者用C是什么意思? [英] What does this ">>=" operator mean in C?

查看:136
本文介绍了这是什么">> = QUOT;操作者用C是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

unsigned long set;
/*set is after modified*/
set >>= 1;

我发现这个内核中的系统调用,但我不明白,它是如何工作的?

I found this in a kernel system call but I don't understand, how does it work?

推荐答案

这位前pression 集>> = 1; 办法设置=集>> 1; 的右移位数设置 1 (自我分配形式>> 按位向右移位运算符检查的按位移位操作)。

The expression set >>= 1; means set = set >> 1; that is right shift bits of set by 1 (self assigned form of >> bitwise right shift operator check Bitwise Shift Operators).

假设,如果设置是:

BIT NUMBER    31   n=27        m=17                 0
              ▼    ▼           ▼                    ▼
set =         0000 1111 1111 1110 0000 0000 0000 0000

再经过集>> = 1; 变量设置变成了:

BIT NUMBER    31   n=26        m=16                 0
              ▼     ▼           ▼                   ▼
set =         0000 0111 1111 1111 0000 0000 0000 0000

注意,比特数移位。

Notice the bits number shifted.

请注意一个有趣的观点:因为设置无符号长所以这个&GT;&GT ; 操作应逻辑移位(<一个href=\"http://stackoverflow.com/questions/2429479/how-do-i-perform-an-unsigned-right-shift-in-java-in-c-c\">unsigned移)逻辑移位并不preserve一些的符号位。

Note a interesting point: Because set is unsigned long so this >> operation should be logical shift( unsigned shift) a logical shift does not preserve a number's sign bit.

此外,因为你正在改变所有位向右(朝向更低显著号),这样一个正确的转变是由两个=鸿沟号。

Additionally, because you are shifting all bits to right (towards lower significant number) so one right shift is = divide number by two.

检查该code (只是为了演示最后一点):

check this code (just to demonstrate last point):

int main(){
 unsigned long set = 268304384UL;
 set >>= 1;
 printf(" set :%lu \n", set);
 set = 268304384UL;
 set /= 2;
 printf(" set :%lu \n", set);
 return 1; 
}

和输出:

 set :134152192 
 set :134152192

(注意:它不会手段&GT;&GT; / 都是一样的)

同样,你有运营商&LT;&LT; = 向左移位,检查其他可用的位运算符复合赋值运算符 ,还要检查节:位前pressions 并区别:的签署/算术移位和无符号转变

Similarly you have operator <<= for left shift, check other available Bitwise operators and Compound assignment operators, also check section: bit expressions and difference between: signed/arithmetic shift and unsigned shift.

这篇关于这是什么&QUOT;&GT;&GT; = QUOT;操作者用C是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆