I have an array of signed ints, and I would like to divide by 2^n.
I currently use this routine to do it:
int out = in >= 0 ? int >> n : -((int & 0x7FFFFFFF) >> n);
Is there a better way that does not involve a branch?
int minus16 = -16;
uint minus16u = -16;
int iResult = minus16 >> 2; (gives 0xfffffffc = -4)
uint uResult = minus16u >> 2; (gives 0x3ffffffc = 1073741820)
So compiler automatically uses arithmetic shift for negative values.