I am trying to figure out how exactly arithmetic bit-shift operators work in C, and how it will affect signed 32-bit integers.
To make things simple, let\'s say we w
As others said shift of negative value is implementation-defined.
Most of implementations treat signed right shift as floor(x/2N) by filling shifted in bits using sign bit. It is very convenient in practice, as this operation is so common. On the other hand if you will shift right unsigned integer, shifted in bits will be zeroed.
Looking from machine side, most implementations have two types of shift-right instructions:
An 'arithmetic' shift right (often having mnemonic ASR or SRA) which works as me explained.
A 'logic' shift right (oftem having mnemonic LSR or SRL or SR) which works as you expect.
Most of compilers utilize first for signed types and second for unsigned ones. Just for convenience.