I need to sign-extend an 8bit value to 12 bits. In C, I can do it this way. I read Apple's BinaryInteger protocol documentation, but it didn't explain sign extending to a variable number of bits (and i'm also pretty new at Swift). How can I do this in Swift, assuming val is UInt8 and numbits is 12?

```
#define MASKBITS(numbits) ((1 << numbits) - 1)

#define SIGNEXTEND_TO_16(val, numbits)              \        
(                                                         \
(int16_t)((val & MASKBITS(numbits)) | (                 \
  (val & (1 << (numbits-1))) ? ~MASKBITS(numbits) : 0)  \
))

```