Numbers are either signed or unsigned. Unsigned numbers don’t have a positive or negative sign, i.e., they are only positive. In the context of computer systems, unsigned numbers increase the absolute value of numbers we can represent in data. In a -bit system, we can represent individual numbers from 0 to .

A key question is with how we can represent signed numbers in binary. The most popular approach involves two’s complement, where the most significant bit is a sign bit.1 Observe how our number representation here still lines up with reality. If we add a positive and a negative number together, we get the correct result! Really cool. We can represent numbers in a -bit system in the interval: .

And how do we find the negative equivalent of a number? We can flip all the individual bits and add 1:

Sign extension

What about a sign extension, like what we get when we type cast? When we convert from an int (32-bits) to a long (64-bits), we do a sign extension and replicate the most significant bits. Here’s an example for a 6-bit to 8-bit conversion:

We can still do our equivalence check from above to verify this holds.

In hardware, sign extension is done by branching off the most significant bit into more wires. For example:

Footnotes

  1. From Prof Anderson’s lecture notes.