Sign Extension

This is a very common thing done in Computer Architecture, that I got introduced to in ECE222.

Sign-extend: To increase the size of a data item by replicating the high-order sign bit of the original data item in the high-order bits of the larger, destination data item.

In Practice

To sign extend, just take the most significant bit and place them left of the word as much as possible. This is for Two’s Complement representation.

Ex: 16-bit binary representation of 2 is

  • 00000000 00000010, so the MSB is 0
  • 32-bit version is 00000000 00000000 00000000 00000010

On the other hand, the 16-bit binary representation is

  • 1111 1111 1111 1110, so the MSB is 1
  • 32-bit version is 11111111 11111111 11111111 11111110
title: Why does this trick work?
This trick works because positive two’s complement numbers really have an infinite number of 0s on the left and negative two’s complement numbers have an infinite number of 1s. The binary bit pattern representing a number hides leading bits to fit the width of the hardware; sign extension simply restores some of them.