Sometimes you want to represent signed numbers, sometimes you don't. But it can be a hassle to convert between them, and having to deal with both might require different instructions. It's also handy to be able to quickly tell if a number's positive or negative. Two's complement notation solves this by not requiring any conversion, by allowing you to use the same operations on both types of number, and having a single bit of the number tell the sign.

How it works

In an 8-bit unsigned representation, you can represent any number from 0 to 255. You should understand that already before attempting to understand this.

In two's complement notation, the numbers 0 to 127 have exactly the same representation as the unsigned version: %00000000 = $00 = 0 up to %01111111 = $7f = 127. Thus, you don't need to do any conversion between signed and unsigned numbers (so long as the unsigned number is less than 128; bigger numbers can't be represented anyway). This gives the entire positive range; you may notice this is all values with the high bit set to 0.

The negative numbers are represented by the high bit being 1. -1 is represented by %11111111 = $ff, and increasingly negative numbers (-2, -3, ...) are represented by increasingly smaller binary numbers ($fe, $fd,...).

BinaryHex2's complement

Because of the continuous representation around the $ff to 00 boundary, normal unsigned operations give correct results with two's complement numbers. All you really need is some way to know when the number breaks the -128/+127 boundary, which is given by the overflow flag.

How to stop worrying and learn to love two's complement

Two's complement can be horribly confusing. You don't have to even consider it most of the time when coding on the Z80; you can just ignore it and treat all your numbers as unsigned and you're unlikely to meet any insurmountable problems. However, when you come to a situation where you need it, just give it a go without thinking too much and chances are, you'll find it does exactly what you want with little effort.

Return to top