I need to implement a 32-bit arithmetic right shift from logical shifts, and, or, xor and normal integer arithmetic operations.
I read somewhere the following is supposed to work:
(x>>N)|(((1<<N)-1)<<(32-N))
x is the integer that will be shifted and N is the amount of bits to shift.
This works for negative (msb is 1) numbers but not for positive numbers (msb is 0).
Does anyone know an efficient algorithm that always produces the right result?