How do you sign extend an 11 bit number to a 32 bit number in C++?
I have the following function, but it is not returning the proper sign-extension as expected:
unsigned int signExtend11to32ui(int i) {
int mask = 2047; // 0x7FF or 0000 0111 1111 1111
return static_cast<unsigned int>(static_cast<int>(i & mask));
}
The following function works correctly for sign-extension of 16 bits to 32 bits:
unsigned int signExtend16to32ui(short i) {
return static_cast<unsigned int>(static_cast<int>(i));
}