Number Systems
![]() | |
| |
Positional number systems
Our decimal number system is known as a positional number system, because the value of the number depends on the position of the digits. For example, the number 123 has a very different value than the number 321, although the same digits are used in both numbers.
(Although we are accustomed to our decimal number system, which is positional, other ancient number systems, such as the Egyptian number system were not positional, but rather used many additional symbols to represent larger values.)
In a positional number system, the value of each digit is determined by which place it appears in the full number. The lowest place value is the rightmost position, and each successive position to the left has a higher place value.
In our decimal number system, the rightmost position represents the "ones" column, the next position represents the "tens" column, the next position represents "hundreds", etc. Therefore, the number 123 represents 1 hundred and 2 tens and 3 ones, whereas the number 321 represents 3 hundreds and 2 tens and 1 one.
The values of each position correspond to powers of the base of the number system. So for our decimal number system, which uses base 10, the place values correspond to powers of 10:
| ... | 1000 | 100 | 10 | 1 |
| ... | 10^3 | 10^2 | 10^1 | 10^0 |
Converting from other number bases to decimal
Other number systems use different bases. The binary number system uses base 2, so the place values of the digits of a binary number correspond to powers of 2. For example, the value of the binary number 10011 is determined by computing the place value of each of the digits of the number:
| 1 | 0 | 0 | 1 | 1 | the binary number |
| 2^4 | 2^3 | 2^2 | 2^1 | 2^0 | place values |
So the binary number 10011 represents the value
| (1 * 2^4) | + | (0 * 2^3) | + | (0 * 2^2) | + | (1 * 2^1) | + | (1 * 2^0) | |
| = | 16 | + | 0 | + | 0 | + | 2 | + | 1 |
| = | 19 | ||||||||
The same principle applies to any number base. For example, the number 2132 base 5 corresponds to
| 2 | 1 | 3 | 2 | number in base 5 |
| 5^3 | 5^2 | 5^1 | 5^0 | place values |
So the value of the number is
| (2 * 5^3) | + | (1 * 5^2) | + | (3 * 5^1) | + | (2 * 5^0) | |
| = | (2 * 125) | + | (1 * 25) | + | (3 * 5) | + | (2 * 1) |
| = | 250 | + | 25 | + | 15 | + | 2 |
| = | 292 | ||||||
Converting from decimal to other number bases
In order to convert a decimal number into its representation in a different number base, we have to be able to express the number in terms of powers of the other base. For example, if we wish to convert the decimal number 100 to base 4, we must figure out how to express 100 as the sum of powers of 4.
| 100 | = | (1 * 64) | + | (2 * 16) | + | (1 * 4) | + | (0 * 1) | |
| = | (1 * 4^3) | + | (2 * 4^2) | + | (1 * 4^1) | + | (0 * 4^0) | ||
| Then we use the coefficients of the powers of 4 to form the number as represented in base 4: | |||||||||
| 100 | = | 1 2 1 0 | base 4 | ||||||
One way to do this is to repeatedly divide the decimal number by the base in which it is to be converted, until the quotient becomes zero. As the number is divided, the remainders - in reverse order - form the digits of the number in the other base.
Example: Convert the decimal number 82 to base 6:
| 82/6 | = | 13 | remainder 4 |
| 13/6 | = | 2 | remainder 1 |
| 2/6 | = | 0 | remainder 2 |
The answer is formed by taking the remainders in reverse order: 2 1 4 base 6
1.5 Binary arithmetic
Converting hexadecimal to decimalNegative numbers
Addition
Binary addition follows the same rules as decimal addition.
Working from the right:
213 246
+ 142 + 127
355 373
If the sum of 2 digits produces a 2 digit result, the digit on the right is written down and the digit on the left is added to the next column to the left.
Decimal addition carry the ten example
The hardest part is remembering to think in binary.
The rules for binary arithmetic are shown in the table below.
0 + 0 = 0
1 + 0 = 1
1 + 1 = 10
1 + 1 + 1 = 11
When a value is represented using sign and magnitude, the left hand bit, the most significant bit, is used solely to determine the sign of the number.
most significant bit = 1 negative number
most significant bit = 0 positive number
For example:
1010 = -2
0010 = +2
This means that a 4-bit number cannot represent any value greater than 7 because the 4th bit is used to indicate the sign. However the 4 bits can still represent 15 different values when the negative numbers are included. These values are:
-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4 , 5, 6, 7
If there was no sign bit, 4 bits would represent the following 16 values:
0, 1, 2, 3, 4, 5 , 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
Question
Can you work out why there are only 15 values when you use sign and magnitude but 16 when there is no sign bit?
Answer
If you write out all the values for both representations, you'll see that there are two different versions of zero in sign and magnitude (0 = 0000 and -0 = 1000). These mean exactly the same.
This is one of the reasons why two's complement is used instead of sign and magnitude.
Decimal arithmetic uses different operations for addition and subtraction. Using two's complement, subtraction is carried out using the machine operation for addition. This system works for bit strings of any length. The examples given will use 8 or 4-bit strings for simplicity but a working PC is likely to use 32 or 64-bit strings. The first bit is again used to indicate a negative value but it also bears the position value.
The two's complement number 1000 1010 is evaluated as follows:
1000 1010
= -128 +0+0+0+8+0+2+0
= -118
Converting a number to its negative form is a two stage process:
1. complement (invert) all the bits (the result at this stage is known as one's complement).
2. add 1.
If you're working with 8-bit strings all values must appear as 8 bits and the possible values will be in the range -128 to 127.
Example 1 - Using a 4-bit binary string
To convert 1101 (1310) to its negative:
.write the number in 8 bit format 0000 1101
invert the bits 1111 0010
add 1 0000 0001
1111 0011
It is easy to check the answer:
1111 0011
= -128 +64 +32 +16 +0 +0 +2 +1
= -128 + 115
= -13
Activity Simulation of 4-bit number conversion to two's complement
The table below shows the two's complement representation of the range -8.. + 7
Decimal Binary two's complement representation
7 0111
6 0110
5 0101
4 0100
3 0011
2 0010
1 0001
0 0000
-1 1111
-2 1110
-3 1101
-4 1100
-5 1011
-6 1010
-7 1001
-8 1000
When 1 is added to the binary representation for -1 the result is a 5 bit number. Because the machine has only allocated 4 bits, the 5th bit is ignored as overflow. Adding one to any number leads to a continuous cycle of 4 bit numbers. The mileometer in a car works in the same way.
1111
+ 0001
1 0000
The processor treats subtraction like the addition of a negative number.
A - B = A + (-B)
All examples will be shown using 8 bit two's complement strings.
Subtraction is a 3 stage process:
1. invert the number to be subtracted;
2. add 1;
3. perform addition.
Example 1 - Subtraction of 19 from 53
To evaluate 53 - 19
Step 1 19 0001 0011
invert 1110 1100
Step 2 add 1 0000 0001
1110 1101
Step 3 add 53 and -19 0011 0101
1110 1101
1 0010 0010
The bit on the left is overflow and is not included in the result. So the result of 53 - 19 = 0010 0010.
You should always check the answer by converting it to decimal.
0010 0010 32 + 2 = 34
53 - 19 = 34
SCHOLAR Heriot-Watt University
searchsearchdiscussiondiscussionFAQFAQreportsreportsActivities in unit 1 Activities in unit 1 Heriot-Watt University
Open Topic Outline SCHOLAR : Computing : 1 Data representation and number systems :
Heriot-Watt University
metadata1.6 Fixed Point Numbers
SubtractionFloating Point Numbers
Fixed point numbers are used, for example, when a set of data values is defined as currency. This would mean that each number would have 2 positions after the decimal point. Fixed point calculations are performed very quickly.
Look at the animation below which illustrates how a decimal number is converted to binary.
You know how to represent positive integers (whole numbers) in binary. Extending the logic to include fixed point numbers is straightforward.
24 23 22 21 20 . 2-1 2-2 2-3
16 8 4 2 1 .
1 0 1 . 1 1 0
= 7.7510
Moving from left to right each position value is achieved by dividing the one on its left by two.
Example 1
Convert 0.375 to binary
24 23 22 21 20 . 2-1 2-2 2-3
16 8 4 2 1 .
0 . 0 1 1
= 0.375
1. Find the largest position value that is less than 0.375 (in this case 0.25);
2. Subtract 0.25 from 0.375 to give ;
3. Put a 1 in the 2-3 position;
4. Put 0 in the other columns.


0 Comments:
Post a Comment
<< Home