Выбрать главу

This magic number is the two’s complement of 23 and finding it is very simple.

How to find the two’s complement of any binary number

Invert each bit, then add 1 to the answer

All we have to do is to take the number we want to subtract (in its binary form) and invert each bit so every one becomes a zero and each zero becomes a one. Note: technically the result of this inversion is called the ‘one’s complement’ of 23. The mechanics of doing it will be discussed in the next chapter but it is very simple and the facility is built into all microprocessors at virtually zero cost.

Converting the 23 into a binary number gives the result of 000101112 (using eight bits). Then invert each bit to give the number 111010002 then add 1. The resulting number is then referred to as the ‘two’s complement’ of 23.

Introduction to Microprocessors and Microcontrollers In this example, we used 8-bit numbers but the arithmetic would be exactly the same with 16 bits or indeed 32 or 64 bits or any other number.

Doing the sum

We now simply add the 50 and the two’s complement of 23:

50 + (the two’s complement of 23) = 27

The answer is 100011011.

Count the bits. There are nine! We have had a carry in the last column that has created a ninth column. Inside the microprocessor, there is only space for eight bits so the ninth one is not used. If we were to ask the microprocessor for the answer to this addition, it would only give us the 8-bit answer: 000110112 or in denary, 27. We’ve done it! We’ve got the right answer!

It was quite a struggle so let’s make a quick summary of what we did.

1 Convert both numbers to binary.

2 Find the two’s complement of the number you are taking away.

3 Add the two numbers.

4 Delete the msb of the answer.

Done.

A few reminders

1 Only find the two’s complement of the number you are taking away – NOT both numbers.

2 If you have done the arithmetic correctly, the answer will always have an extra column to be deleted.

3 If the numbers do not have the same number of bits, add leading zeros as necessary as a first job. Don’t leave until later. Both of the numbers must have the same number of bits. They can be 8-bit numbers as we used, or 16, or 32 or anything else so long as they are equal.

A quick way to find the two’s complement of a binary number

Start from the left-hand end and invert each bit until you come to the last figure 1. Don’t invert this figure and don’t invert anything after it.

Example 1

What is –2410 expressed as an 8-bit two’s complement binary number?

1 Change the 2410 into binary. This will be 11000.

2 Add leading zeros to make it an 8-bit number. This is now 00011000.

3 Now start inverting each bit, working from the left until we come to the last figure ‘1’. Don’t invert it, and don’t invert the three zeros that follow it.

Example 2

What is –10010 expressed as a 16-bit two’s complement binary number?

1 Convert the 10010 into binary. This gives 11001002.

2 Add nine leading zeros to make the result the 16-bit number 0000000001100100.

3 Now, using the quick method, find the two’s complement:

The result is 1111 1111 1001 1100

Example 3

Find the value of 1011 01112–00 10112 using two’s complement addition.

1 The second number has only six bits so add two zeros on the lefthand end to give 1011 0111–0000 1011.

2 Invert each bit in the number to be subtracted to find the one’s complement. This changes the 00001011 to 11110100.

3 Add 1 to give the two’s complement: 11110100+1=11110101 (or do it the quick way).

4 Add the first number to the two’s complement of the second number:

5 The result so far is 110101100 which includes that extra carry so we cross off the msb to give the final answer of 101011002.

Floating point numbers

Eight-bit numbers are limited to a maximum value of 111111112 or 25510. So, 0–255 means a total of 256 different numbers. Not very many. 32-bit numbers can manage about 4¼ billion. This is quite enough for everyday work, though Bill Gates’ bank manager may still find it limiting. The problem is that scientific studies involve extremely large numbers as found in astronomy and very small distances as in nuclear physics.

So how do we cater for these? We could wait around for a 128-bit microprocessor, and then wait for a 256-bit microprocessor and so on. No, really, the favorite option is to have a look at alternative ways of handling a wide range of numbers. Rather than write a number like 100 we could write it as 1×10². Written this way it indicates that the number is 1 followed by two zeros and so a billion would be written as 1×109. In a similar way, 0.001 is a 1 preceded by two zeros would be written as 1×10–3 and a billionth, 0.000000001, would be 1×10–9. The negative power of ten is one greater than the number of zeros. By using floating point numbers, we can easily go up to 1×1099 or down to 1×10–99 without greatly increasing the number of digits.

Fancy names

Normalizing

Changing a number from the everyday version like 275 to 2.75×10² is called normalizing the number. The first number always starts with a single digit between 1 and 9 followed by a power of ten. In binary we do the same thing except the decimal point is now called a binary point and the first number is always 1 followed by a power of two as necessary.

Three examples

1 Using the same figure of 275, this could be converted to 100010011 in binary. This number is normalized to 1.00010011×28.

2 A number like 0.00010012 will have its binary point moved four places to the right to put the binary point just after the first figure 1 so the normalized number can be written as 1.001×2–4.

3 The number 1.1012 is already normalized so the binary point does not need to be moved so, in a formal way, it would be written as 1.101×20.

A useless fact

Anything with a power of zero is equal to 1. So 20=1, 100=1. It is tempting but total nonsense to use this fact to argue that since 20=1 and 100=1 then 2 must equal 10!

Terminology

There are some more fancy names given to the parts of the number to make them really scary.

The exponent is the power of ten, in this example, 9. The mantissa, or magnitude, is the number, in this case 8.0245. The radix is the base of the number system being used, 2 for binary, 16 for hex, 10 for decimal.

Storing floating point numbers

In a microprocessor, the floating point is a binary number. Now, in the case of a binary number, the mantissa always starts with 1 followed by the binary point. For example, a five digit binary mantissa would be between 1.0000 and 1.1111.

Since all mantissas in a binary system start with the number 1 and the binary point, we can save storage space by missing them out and just assuming their presence. The range above would now be stored as 0000 to 1111.

It is usual to use a 32-bit storage area for a floating point number. How these 32 bits are organized is not standardized so be careful to check before making any assumptions. Within those 32 bits, we have to include the exponent and the mantissa which can both be positive or negative. One of the more popular methods is outlined below.