Two`s complement and BCD

advertisement
Two’s complement
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 used to
indicate a negative value but it is bears the position value.
-1
1111
0
0000
1
0001
-2
1110
2
0010
-3
1101
3
0011
-4
1100
4
0100
-5
1011
5
0101
-6
1010
6
0110
-7
1001
–8
1000
7
0111
The twos complement number 1000 1010 is evaluated as follows:
1000 1010
= -128 + 0 + 0 + 0 + 8 + 0 + 2
= - 118
Copyright © 2003 by Sanjaya Ratnaweera
Converting a number to its negative form
1. Complement (invert) all the bits (the result at this stage is 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
Using a 4-bit string.
Convert 1101 (1310 ) to it’s negative.
Write the number in 8-bit format
Invert the bits
Add 1
0000 1101
1111 0010
0000 0001
1111 0011
Easy to check the answer.
11110011
= -128 + 64 + 32 + 16 + 0 + 0 + 2 + 1
= - 128 + 115
= -13
Copyright © 2003 by Sanjaya Ratnaweera
Binary Coded Decimal (BCD) Number System
The BCD Number system
Now you should be familiar with Binary, Decimal and Hexadecimal Number Systems. If
we view single digit values of hexadecimal, the numbers 0-F, the represent the values
0 – 15 in decimal. Often, we wish to use a binary equivalent of the decimal system. This
system is called Binary Coded Decimal. In BCD, the binary patterns 1010 through 1111
do not represent valid BCD numbers, and cannot be used.
Conversion from Decimal to BCD is straightforward. You merely assign each digit of the
decimal number to a byte and convert 0 through 9 to 0000 0000 through 0000 1001, but
you cannot perform the repeated division by 2 as you did to convert decimal to binary.
Let us see how this works. Determine the BCD value for the decimal number 5319. Since
there are four digits in our decimal number, there are four bytes in our BCD number.
They are
Thousands
Hundreds
Tens
Ones
5
3
1
9
00000101
00000011
00000001
00001001
PACKED BCD
Since storage on disk and in RAM is not valuable, we would like to eliminate this wasted
storage. This may be accomplished by packing the BCD number. In a packed BCD
number, each nibble has a weighted position and starting from the decimal point.
Therefore, instead of requiring 4 bites to store the BCD 5319, we would only require
2 bytes, half the storage. The upper nibble of the upper byte of our number would store
the THOUSANDS value while the lower nibble of the upper byte would store the
HUNDREDS value. Likewise, the lower byte would store the TENS value in the upper
nibble and ONES digit in the lower nibble. Therefore, our previous example would be:
Thousands-Hundreds
Tens-Ones
53
19
01010011
00011001
Copyright © 2003 by Sanjaya Ratnaweera
Download