tags:

views:

136

answers:

2
std::vector<int> a;
int p;
int N;

// ...

p = a[ N>>1 ];

What is the N>>1 part?

+6  A: 

Divides N by 2 (by bit shifting right 1) and using that as the index into the vector a to assign p.

RC
It should probably be mentioned better would be (N / 2). It's more readable and correct.
GMan
+1  A: 

It looks like it sets p to the middle element of a. if a is sorted, it would be the median element, and could be part of a binary search algorithm.

Mike DeSimone
This is incorrect, unless N is equal to the length of a. N >> 1 is the equivalent of an integer division by two.
Justin Smith
Yes, I assumed `N` was the number of elements in `a`, which is pretty standard in the context of math and O() notation.
Mike DeSimone