xp_compute math question

Posted by Robert Powell on Tue 19 Jul 2005 09:15 AM — 2 posts, 12,180 views.

Australia #0
could someone explain to me what the >> 2 does in the following fragment.
xp = (xp*3) >> 2;
USA #1
>> 2 means bitwise shift right by two.

If you have the number 4, which is 100 in binary, then 4 >> 1 is equal to 10 which is 2. 4 >> 2 equals 1 which is 1. 5 in binary is 101, so 5 >> 1 = 2 and 5 >> 2 = 1.

The quick answer is that "x >> 2" is an extremely efficient way to (integer-)divide x by two.