Sign Extension
Convert an N-bit signed value packed inside a larger unsigned variable into a true, native signed integer.
Silicon Logic
In hardware, a negative number is identified by its Most Significant Bit (MSB) being set to
1 (Two's Complement).
However, if a peripheral outputs a 12-bit signed number, the MSB is at bit
index 11. The CPU stores this inside a standard 16-bit or 32-bit register, where bit 11 means
nothing to native math. It just looks like a large positive number.
The Bitwise Trick: We must propagate that 12th bit all the way out to the 32nd
bit.
By shifting the MSB to the extreme absolute-left, then doing a Signed Right Shift, C natively
triggers an ASR (Arithmetic Shift Right) CPU Instruction which mathematically
copies the sign bit across the empty space.