1

I came across this line of code in a reseach paper for "Efficient Integral Image Computation on the GPU":

for (int d = n>>1; d>0; d>>=1){...}

(int) n is an input variable.

I am familiar with for-loops and c++ in general. So I just could figure out the parts n>>1 and d>>=1. I havent seen this before. Could someone provide a short explanation? Thank you.

KabCode
  • 766
  • 8
  • 25
  • 1
    Did you try to google "C++ operators"? It's bound to give you a list with explanation to every operator. – DeiDei May 26 '17 at 11:10
  • Its a bit shifting operator. – Sumeet May 26 '17 at 11:11
  • okay, know I see what it is. I havent seen a shifting operator in a for loop head. That might be my problem. It this common practice or seldom used? – KabCode May 26 '17 at 11:28

1 Answers1

2

>> is the bitwise operator for right shift.

Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

Source

So this:

n >> 1

will move all the bits of n 1 place to the right.

>>= is a right shift assignment operator. It takes that value stored in i and shifts all it's bits to the right 1 place (with the leftmost bit being set to 0)

As a reuslt, this:

d >>= 1

is equivalent to this:

d = d >> 1;
gsamaras
  • 71,951
  • 46
  • 188
  • 305