C语言 这个 ">>=" 运算符在 C 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17769948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does this ">>=" operator mean in C?
提问by Rotom92
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?
我在内核系统调用中发现了这个,但我不明白,它是如何工作的?
回答by Grijesh Chauhan
The expression set >>= 1;means set = set >> 1;that is right shift bits of setby 1(self assigned form of >>bitwise right shift operator check Bitwise Shift Operators).
该表达式的set >>= 1;意思set = set >> 1;是set由1(>>按位右移运算符的自赋值形式检查Bitwise Shift Operators)的右移位。
Suppose if setis:
假设如果set是:
BIT NUMBER 31 n=27 m=17 0
▼ ▼ ▼ ▼
set = 0000 1111 1111 1110 0000 0000 0000 0000
Then after set >> = 1;variable setbecomes:
然后在set >> = 1;变量之后set变成:
BIT NUMBER 31 n=26 m=16 0
▼ ▼ ▼ ▼
set = 0000 0111 1111 1111 0000 0000 0000 0000
Notice the bits number shifted.
注意移位的位数。
Note a interesting point: Because setis unsigned longso this >>operation should be logical shift( unsigned shift) a logical shift does not preserve a number's sign bit.
注意一个有趣的观点:因为set是unsigned long所以这个>>操作应该是逻辑移位(无符号移位)逻辑移位不能保持数的符号位。
Additionally, because you are shifting all bits to right (towards lower significant number) so one right shift is = divide number by two.
此外,因为您将所有位右移(朝向较低的有效数字),所以右移是 = 将数字除以二。
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;
}
And output:
和输出:
set :134152192
set :134152192
(note: its doesn't means >>and /are both same)
(注意:它并不意味着>>并且/都相同)
Similarly you have operator <<=for left shift, check other available Bitwise operatorsand Compound assignment operators, also check section: bit expressionsand difference between: signed/arithmetic shift and unsigned shift.
同样,您有左移运算符<<=,请检查其他可用的按位运算符和复合赋值运算符,还要检查部分:位表达式以及以下之间的区别:有符号/算术移位和无符号移位。
回答by PP.
This "right-shift"s the value by one bit. If you move all the bits of an integer to the right by 1 then you effectively "divide by 2" because binary is a base-2 numbering system.
这个“右移”值一位。如果将整数的所有位向右移动 1,则实际上可以“除以 2”,因为二进制是基数为 2 的编号系统。
Imagine you have the number 12 in binary:
假设您有二进制数字 12:
1100 = 12 in binary
110 = 6 in binary (1100 right-shifted)
Just like if you moved all of the digits in a base-10 number right by one you would be dividing by 10.
就像如果您将基数为 10 的数字中的所有数字右移一个,您将被除以 10。
回答by Ashish Kaila
This shifts bit to the right by 1 which is equivalent to division by 2. For more information on bit shifting, refer to http://msdn.microsoft.com/en-us/library/f96c63ed(v=vs.80).aspx
这会将位右移 1,这相当于除以 2。有关位移位的详细信息,请参阅http://msdn.microsoft.com/en-us/library/f96c63ed(v=vs.80)。 aspx
回答by Barmar
Every binary operator can be combined with =. In all cases
每个二元运算符都可以与=. 在所有情况下
dest op= expression
is equivalent to
相当于
dest = dest op expression
(except if desthas any side effects, they only take place once).
(除非dest有任何副作用,它们只发生一次)。
So this means that
所以这意味着
set>>=1;
is equivalent to:
相当于:
set = set >> 1;
Since >>is the binary right-shift operator, it means to shift the value in setright by 1 bit.
由于>>是二元右移运算符,因此表示将值set右移 1 位。
回答by Santhosh Pai
The above command performs right shift by one bit .Refer bit wise operations in c from this link http://www.cprogramming.com/tutorial/bitwise_operators.html
上面的命令执行一位右移。从这个链接http://www.cprogramming.com/tutorial/bitwise_operators.html 参考c 中的按位操作

