I have a unsigned integer (32 bit), in which I've stored an n-bit signed number (2's complement, so -16 ≤ x < 16, and the resultant unsigned value is in the range 0-15).
How can I transform this efficiently into a signed interpretation using the two's complement?
A short example to clarify what I mean:
int numUnsigned = 15; // Store a 4-bit value 0b1111
int numSigned = ???; // Convert to 4-bit signed value using two's complement
// Now numSigned should be -1, since 0b1111 == -1
I've been messing with the bits all morning but can't seem to get it right.