How can I perform arithmetic right shift in C in a portable way?
We are writing an emulator where we need sign propagating right shift. The emulated system uses 2's complement numbers. I read that the >> operator on signed integers in C is implementation defined. So I cannot rely on the fact it will result in the correct bit pattern in all platforms. This means I'll need to use bit manipulation to reproduce the arithmetic right shift, and I would want to avoid unnecessary branching if possible. EDIT: In response to a comment: "The missing bit is that OP needs to define what result is "correct" when the sign bit is set in x with x >> y" I basically want to