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 right shift assignment operator ( >>= ) moves the specified amount of bits to the right and assigns the result to the variable.
Bitwise Right shift operator >> is used to shift the binary sequence to right side by specified position.
When shifting an unsigned value, the >> operator in C is a logical shift. When shifting a signed value, the >> operator is an arithmetic shift.
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.
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
Literally offset = offset >> 1
, that is, offset
divided by 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With