Sign extension (source code)

= Sign extension
{title2=$\operatorname{sext}_{m\to w}(b)$}
{wiki}

To widen an $m$-<bit> <two's complement> <integer> to $w>m$ <bits>, preserve the original low <bits> and fill the new high positions with the old sign <bit> $s$. The change in signed positional weights is $-s2^{w-1}+s\sum_{i=m-1}^{w-2}2^i=-s2^{m-1}$, so the represented signed value is unchanged. Zero extension instead preserves the <unsigned interpretation>, and changes the signed value of negative inputs.