I\'ve got the following task:
Compute x/(2^n), for 0 <= n <= 30 using bit shifting. Requirement: Round toward zero
Compute x/(2^n), for 0 <= n <= 30 using bit shifting.
x/(2^n)
0 <= n <= 30
Requirement: Round toward zero
public int DivideByPowerOf2(int x, int n) { return (x + ((x >> 31) & ((1 << n) + ~0))) >> n; }