简体   繁体   English

>> =在C或C ++中是什么意思?

[英]What is the meaning of >>= in C or C++?

What is the meaning of the >>= symbol in C or C++? 在C或C ++中>>=符号的含义是什么? Does it have any particular name? 有什么特别的名字吗?

I have this for loop in some CUDA code which looks like this 我在一些看起来像这样的CUDA代码中有这个for循环

for(int offset=blockDim.x; offset>0; offset >>=1)
{
   //Some code 
}

How does the offset variable get modfied with the >>= operator? 如何使用>>=运算符修改offset变量?

The >>= symbol is the assignment form of right-shift, that is x >>= y; >>=符号是右移的赋值形式,即x >>= y; is short for x = x >> y; x = x >> y;缩写x = x >> y; (unless overloaded to mean something different). (除非过载意味着不同)。

Right shifting by 1 is equivalent to divide by 2. That code looks like someone doesn't trust the compiler to do the most basic optimizations, and should be equivalent to: 右移1等于除以2。该代码看起来好像有人不信任编译器执行最基本的优化,并且应等效于:

for( int offset = blockDim.x; offset > 0; offset /= 2 ){ ... }

More information about bitwise operations here: 有关按位运算的更多信息,请参见:

http://en.wikipedia.org/wiki/Binary_shift#Bit_shifts http://en.wikipedia.org/wiki/Binary_shift#Bit_shifts

从字面上看, offset = offset >> 1 ,即offset除以2

这是右移的赋值版本:

foo >>= 2; // shift the bits of foo right by two places and assign the result to foo

it's a bitwise shift right operator. 这是按位右移运算符。 it shifts the bits of the variable to right by the value of right operand. 它将变量的位右移右操作数的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM