It is assumed that you already know how to convert unsigned numbers as follows. Otherwise visit the indicated review tutorials:
If one system is used to record the information and a different system is used to read the information, then the result will be confusion.
A discrete unencrypted coding system breaks the information to be
recorded down into a series of independent symbols from some alphabet and then
replaces each symbol by a binary code using the same code table for each symbol.
ASCII text
is
an example.
A coding system is said to be fixed length if the binary code for every symbol has the same length. For example in the ASCII code, each character has a 7 bit code and so the code length is 7 bits.
Given an alphabet coded by n bits in a fixed length binary coding system. Then that alphabet can have at most 2n symbols in it. This is called the Power Rule.
number of symbols in alphabet = 2number of bits in code
For example, the ASCII
character
alphabet has 128 = 27 characters in it because the ASCII code uses
seven bits.
Before considering the completely general twos complement system it will be useful to look at a simple version of that system.
When positive decimal numbers and zero are coded using 4 bit binary numbers, then there is a coding system implied. It is the unsigned 4 bit integer coding system. According to the power rule mentioned above, four bits of code allows only sixteen different symbols in the alphabet - in this case just sixteen different number values. The coding table is as follows:
|
0 = 0000
|
4 = 0100
|
8 = 1000
|
12 = 1100
|
|
1 = 0001
|
5 = 0101
|
9 = 1001
|
13 = 1101
|
|
2 = 0010
|
6 = 0110
|
10 = 1010
|
14 = 1110
|
|
3 = 0011
|
7 = 0111
|
11 = 1011
|
15 = 1111
|
This is the familiar decimal to unsigned
binary conversion table
seen in the Base n Notation tutorial.
However, we wish to code negative numbers as well as positive and zero;
and we
wish to keep to a 4 bit code, but the Power Rule
says that we can only have 16 codes and so:
Some of the positive number codes must be given up to make room for negative number codes.
There are several ways of doing this (signed magnitude, excess, ones complement). We will learn the twos complement coding system. The general theory will come later, but for right now, here is the signed twos complement 4 bit integer coding system for representing both positive and negative decimal numbers.
|
0 = 0000
|
4 = 0100
|
-8 = 1000
|
-4 = 1100
|
|
1 = 0001
|
5 = 0101
|
-7 = 1001
|
-3 = 1101
|
|
2 = 0010
|
6 = 0110
|
-6 = 1010
|
-2 = 1110
|
|
3 = 0011
|
7 = 0111
|
-5 = 1011
|
-1 = 1111
|
The general n bit signed twos complement coding system can be understood by generalizing the 4 bit system above.
Let n denote the number of bits each code will have. Then let M denote the maximum number of possible codes. According to the Power Rule, this must be:
M = 2n
An algorithm for converting a signed decimal number to twos complement signed binary can be written combining the ideas of steps 8 and 9 of the previous section:
An algorithm for converting a signed decimal number to twos complement signed hex can be written combining the ideas of steps 8 and 9 of the previous section:
An algorithm for converting a signed binary number from two complement to signed decimal can be written combining the ideas of steps 6 and 7 of the previous section:
An algorithm for converting a signed hexidecimal number from two complement to signed decimal can be written combining the ideas of steps 6 and 7 of the previous section:
The "negative" operation is a unary operation for changing the sign of a number.
One way to change the sign for a signed binary number is to decode it to decimal, change the sign of the decimal number, and then code it back to signed binary. That is a lot of work
As an example using 5 bit signed binary (twos complement understood), consider the binary number 10101. Here are the steps just discussed:
Surely there is a more direct way to change the sign of a signed binary number.
There is - known as the twos complement operation. Here is how the above example could have been done directly.
You need to know how to take the ones complement. Fortunately that is very easy;
To take the one's complement, just switch all the bits.
In other words, change all 0's to 1's and all 1's to 0's.
You also must be able to add one in binary and in general, addition is something you will learn later. However adding one can be pretty easy. The example above was simple:
01010
+ 1
-------
01011
but in general it can get more complicated - for example:
00111
+ 1
-------
01000
SO, until you learn addition, here is the shortcut algorithm for doing the twos
complement:
Add 5 to register aIn a more program like notation this would be:
a=a+5and if this is coded in 8 bit ascii hex notation we get:
61 3D 61 2B 35When the ECTC98 computer (which we will study later) attempts to execute this information, it will see two and a half instructions which tell it to:
61 3D And the content of memory cell #3D into register b 61 2B And the content of memory cell #2B into register b 35 ?? Add the number ?? to register bwhich is not what was intended at all.
Copyright © 1998 Dr. James F. Wirth