Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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?

like image 860
smilingbuddha Avatar asked Nov 08 '11 21:11

smilingbuddha


People also ask

What does the >>= operator do?

The right shift assignment operator ( >>= ) moves the specified amount of bits to the right and assigns the result to the variable.

What does >> represent in C?

Bitwise Right shift operator >> is used to shift the binary sequence to right side by specified position.

Is >> in C logical or arithmetic?

When shifting an unsigned value, the >> operator in C is a logical shift. When shifting a signed value, the >> operator is an arithmetic shift.

What does >>= in python mean?

In Python >> is called right shift operator. It is a bitwise operator. It requires a bitwise representation of object as first operand. Bits are shifted to right by number of bits stipulated by second operand.


2 Answers

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

like image 123
K-ballo Avatar answered Oct 02 '22 20:10

K-ballo


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

like image 37
alf Avatar answered Oct 02 '22 21:10

alf