smilingbuddha
smilingbuddha

Reputation: 14660

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?

Upvotes: 5

Views: 648

Answers (4)

JosephH
JosephH

Reputation: 8815

it's a bitwise shift right operator. it shifts the bits of the variable to right by the value of right operand.

Upvotes: 1

Caleb
Caleb

Reputation: 125007

That's the assignment version of right shift:

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

Upvotes: 1

alf
alf

Reputation: 8513

Literally offset = offset >> 1, that is, offset divided by 2

Upvotes: 1

K-ballo
K-ballo

Reputation: 81349

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

Upvotes: 16

Related Questions