简体   繁体   中英

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

What is the meaning of the >>= symbol in C or C++? Does it have any particular name?

I have this for loop in some CUDA code which looks like this

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

How does the offset variable get modfied with the >>= operator?

The >>= symbol is the assignment form of right-shift, that is x >>= y; is short for 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:

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

More information about bitwise operations here:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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