Lets say I have a integer (32 bit), which stores a n-bit unsigned number (with n < 32). 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 0b111 == -1
I've been messing with the bits all morning but can't seem to get it right.