[英]What does this “>>=” operator mean in 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? 我在内核系统调用中发现了这个,但我不明白,它是如何工作的?
The expression set >>= 1;
表达式
set >>= 1;
means set = set >> 1;
表示
set = set >> 1;
that is right shift bits of set
by 1
(self assigned form of >>
bitwise right shift operator check Bitwise Shift Operators ). 这是
set
为1
右移位(自动分配形式的>>
按位右移运算符检查按位移位运算符 )。
Suppose if set
is: 假设如果
set
为:
BIT NUMBER 31 n=27 m=17 0
▼ ▼ ▼ ▼
set = 0000 1111 1111 1110 0000 0000 0000 0000
Then after set >> = 1;
然后
set >> = 1;
variable set
becomes: 变量
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 set
is unsigned long
so 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. 此外,因为您将所有位向右移动(向较低有效数字移动)所以右移是=将数字除以2。
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 operators and Compound assignment operators , also check section: bit expressions and difference between: signed/arithmetic shift and unsigned shift . 类似地,您有左移位运算符
<<=
,检查其他可用的按位运算符和复合赋值运算符 ,还检查部分: 位表达式和之间的区别:有符号/算术移位和无符号移位 。
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。
这会将位向右移1,相当于除以2.有关位移的更多信息,请参阅http://msdn.microsoft.com/en-us/library/f96c63ed(v=vs.80)。 ASPX
Every binary operator can be combined with =
. 每个二元运算符都可以与
=
组合。 In all cases 在所有情况下
dest op= expression
is equivalent to 相当于
dest = dest op expression
(except if dest
has 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 set
right by 1 bit. 由于
>>
是二进制右移运算符,它意味着set
右侧的值移位1位。
上面的命令执行右移一位。从此链接中查看 c中的位操作http://www.cprogramming.com/tutorial/bitwise_operators.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.