Computer Binary

Twos Complement Notation

There are two differences between binary numerals written on paper and as used in computers. First, computer numbers are always stored as a definite number of bits. Often this is 16 or 32 or sometimes 64 bits. To write this on paper we include enough leading 0s to make up the correct number of digits. A consequence is that only a limited range of numbers can be represented. It is quite possible for a sum to result in a value which is too big (or too small) to be represented in the word length. Hence in addition to the pattern of bits representing the answer of a sum, there is also an indication of whether that answer is correct.

Second, a computer number only has two signs 0 and 1. When we write on paper we also use a space to show the beginning and the end of a numeral, and a - for a negative number. Computer numerals don't need a sign to show the ends for reason one above, but they do need some way to show negative numbers. There are several methods in use, three of which are called: sign and magnitude notation, ones complement notation, and twos complement notation. Computer hardware designers are in principal free to choose any of the methods so long as they can build the hardware to process them. It turns out in practice that twos complement notation gives the slickest designs with the fewest transistors to do the work and so it is used in almost all designs. Unfortunately it is not all that easy to understand.

What you have to learn

Most of the calculations are similar to paper binary calculations. So to begin with the only change is to set out the numerals padded out with leading 0s. When adding positive numerals the only new thing is to recognise the symptoms of when the answer is wrong because it needs too many bits to fit into a word.

The major difficulty is in understanding negative numerals. You have to understand how to convert negative decimals to and from fixed length twos complement binary, how to negate binary numerals, and more elaborate rules for when the answer of a sum is wrong. A benefit is that you no longer have to subtract in binary. It is much easier to negate the number which needs to be added and then add that negation.

Finally there is a new task; converting between different word lengths. Numerals can always be converted to a longer word length, for example from 16 to 32 bits or from 32 bits to 64. Most numerals which need a longer word length won't fit into a shorter word. You should recognise those which can.

For the purposes of learning, these notes will use 4 and 8 bit numerals. 4 bit numerals can only represent numbers between -8 and +7, and 8 bit numerals can deal with the range from -128 to +127. Both ranges are too small for any practical use but using such short ranges allows you to understand the principles without having to accurately write out long lists of 1s and 0s.

Meaning of twos complement numbers,

The trick is that the leftmost bit is given a negative instead of a positive weight. This allows negative numbers to be written. For example in 4 bit twos complement numbers the weights are: 1 on the right, then 2, 4 and finally -8 on the left. Hence the bit patterns have the meanings:

BinaryDecimal BinaryDecimal BinaryDecimal BinaryDecimal
00000 0100 4 1000 -8 1100-4
0001 1 0101 5 1001 -7 1101 -3
0010 2 0110 6 1010 -6 1110 -2
0011 30111 7 1011 -5 1111 -1

Recognising if a numeral is positive or negative

If the leading digit is 0 the number is positive. If it is 1 the number is negative. The reason is that even if all the other bits are also 1s, they don't result in enough positive value to outweigh the leading negative bit. For example, in the above table 1111 is negative. The right three bits add up to 1x4 + 1x2 + 1x1 = 7 which does not outweigh the first bit with a weight of -8.

Adding

Addition of positive numbers

Just do the addition in the normal way except that it is better to pad out the numbers with leading zeros. When you have finished, look at the bit in the leading position of the answer. If it is 0 the answer should be right (so long as you didn't make a mistake.) If it is 1 it can't be right as a leading 1 represents a negative number. The correct answer is too big to fit into the word length.

E.g. 2 + 3 in 4 bit 2s complement binary is 0010 + 0011 = 0101. The leading bit is 0; hence the final pattern represents a positive answer and is correct. It is 5.

E.g. 4 + 5 in 4 bit 2s complement binary is 0100 + 0101 = 1001. The leading bit is 1; hence the final pattern represents a negative answer and is wrong. This is because 4 + 5 = 9 which is too big to be represented in 4 bit 2s complement.

Addition of negative numbers

This is the trick. Write down the numbers in binary. (They will both have leading 1s.) Then just do the addition in the normal way as if they were positive numbers. When you have finished, there will be an extra bit on the left which will be a 1. Throw it away. Look at the bit in the leading position of the answer (The bit 4 position.) If it is 1 the answer should be right (so long as you didn't make a mistake.) If it is 0 it can't be right as a leading 0 represents a positive number. The correct answer is too small to fit into the word length.

E.g. -2 + -3 in 4 bit 2s complement binary is 1110 + 1101 = 11011. Throw away the leading 5th bit leaving 1011. The remaining leading bit is now 1, so the final pattern represents a negative answer and is correct. It is -5.

E.g. -4 + -5 in 4 bit 2s complement binary is 1100 + 1011 = 10111. After removing the leading 1 there remains 0111. The leading bit is 0; hence the final pattern represents a positive answer and is wrong. This is because -4 + -5 = -9 which is too small to be represented in 4 bit 2s complement.

Addition of a positive to a negative number

This again is the trick. Write down the numbers in binary. (One will have a leading 0, the other a leading 1.) Then just do the addition in the normal way as if they were positive numbers. When you have finished, there may or may not be an additional 5th digit. If there is then throw it away and keep only the rightmost 4 bits. The answer is always correct (so long as you didn't make a mistake.)

E.g. 6 + -3 in 4 bit 2s complement binary is 0110 + 1101 = 10011. Throw away the leading 5th bit leaving 0011. This is correct. It is 3.

E.g. -4 + 3 in 4 bit 2s complement binary is 1100 + 0011 = 1111. There are only 4 bits in the answer so no 5th bit to remove. 1111 represents -1 which is the right answer.

Addition - Summary of general rule

Add the two bit patterns as if they were positive numbers. If necessary chop the answer to the rightmost 4 bits to fit the word length. If the two leading bits in the numbers being added are different the answer is right. If they are the same and the leading bit of the answer is the same the answer is right. But if they are the same and the leading bit of the answer is different there is a numerical overflow. The correct answer cannot be represented in the word length. The bit pattern does not represent the true value.

Negation

There are two ways to work out the bit patterns for negative numbers. The first is to set the bits to work out. For example, to work out the bit pattern for -3 in 4 bit twos complement. The number is negative so the left-hand bit is 1 which gives a weight of -8. The remaining bits are positive and have to add on enough to build -8 up to -3. Now (-8 + 5) = -3 so the remaining positive bits have to yield 5 so they are 101 and the total pattern is 1101.

The second method is often slicker. You start by converting the positive form of the number to binary. (In the example +3 gives 0011.) Then you construct what is called the ones complement by inverting all the bits. (Giving 1100.) Then you add 1. (Giving 1101, the same answer as before as it should be.)

This second method works just as well if you start with a negative number. We've just seen that 1101 is -3. Its ones complement is 0010 and then adding 1 give 0011 which is +3. This is a useful check that you've done the method right in the first place.

The reason this works is that if you add a number to its ones complement you get 1111. This represents -1. If you further add 1 you get 0000 after dropping the additional fifth 1 on the left. This answer is right as it came from the sum of a positive and a negative number. This can be written as: (a number + its ones complement) + 1 = 0. By using the associative law we deduce: a number + (its ones complement + 1) = 0. But what is the negation of a number but the number which when added to the original number makes 0. Hence the ones complement + 1 is the negation.

Subtraction

You do not need to learn a special method for subtraction. What you do is to construct the 2s complement of the bit pattern for the number to take away and then add it. For example 5-3 is 5+(-3). The bit pattern for +3 is 0011 so for -3 is 1101. Hence the binary sum is 0101 + 1101 = 0010 (after dropping the 5th bit) which is the code for 2.

This is the reason why hardware designers normally use twos complement arithmetic. They do not need to build a circuit to do subtraction. All they need is a circuit for negation - which is quite simple - and then to reuse the addition circuit.

Changing wordlength

It is easy to write a positive number in a longer wordlength - just pad out the extra most significant bits with 0s. The surprise is for negative numbers you must pad out the most significant bits with 1s. The reason is that it is a different bit which has the negative weight. For example in a 4 bit number the 4th bit has the negative weight -8, but when the numeral is extended to 8 bits the 4th bit now has a positive weight of +8 and it is the 8th bit which has the negative weight of -128. This change in weighting doesn't matter for a positive number which has 0s in the affected positions. It does matter for a negative number. However a 4 bit negative number has 1 in the left position which yields -8 towards the sum. The sign extended 8 bit numeral will have 11111 in the left-hand five places giving weights of -128 + 64 + 32 + 16 + 8. If you add these you get -128 + 120 = -8, the correct total amount.

It is now easy to see if a bit pattern corresponds to a number which will fit into a shorter wordlength. If all the leading bits are the same it will, if not not. For example 11111010 will shorten to 4 bits 1010 as the 5 leading bits are all 1s. (Note that 11111010 is the negation of 00000110 which is +6, so 11111010 is -6. This is in the range representable in 4 bits.) For another example 00001101 does not have the first five bits all the same, only the first four, so it will not shorten. It evaluates to 8+4+1 = 13 which is indeed out of range.