Hi, whats the easiest way to transform a 12 bit unsigned variable into a 16 bit unsigned variable where the additional 4 bits equal to the 12th bit of the original variable? (sign extension)
You can do it using typecasting, when you shift a signed number the sign is preserved (arithmetic shift)
Code C - [expand]
1
2
3
4
volatileunsignedint x;
x=0xF0A;
x=((signedint)x<<4)>>4;// shift left 4 bits so that bit11 goes to bit15 as sign and then shift right 4 and the "sign" will be preserved