8421bcd, ASCII, binary
This section describes the principles of 8421bcd, ASCII, and binary codes and their conversion.
The computer uses binary numbers for computation without exception, but generally reads and writes data in octal and hexadecimal formats. It is no problem for computer technology professionals to understand the meaning of these numbers, but it is not so easy for non-professionals. In daily life, people are most familiar with the decimal system. Therefore, a binary decimal code, called BCD code, is a decimal digit represented in binary.
1. 8421bcd: the decimal number of the binary code.BCD code(Binarycoded decimal). This method uses a combination of 4-bit binary codes to represent 10 decimal numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. There are 16 combinations of 4-digit binary numbers. In principle, you can choose 10 of them as the code, representing 0, 1, 2, 3, 5, 6, 7, 8, 9 in decimal format.
These 10 operators. The most common BCD code is 8421bcd. 8.4.2.1 is the value of 4-bit binary numbers.
1. Conversion between BCD and decimal number: conversion between BCD and decimal number. the relationship is intuitive, and mutual conversion is also very simple. Convert the decimal number 75.4 to BCD code such as: 75.4 = (0111 (0101.0100) BCD if the BCD code 1000 0101.0101 is converted to a decimal number such: (1000 0101.0101) BCD = 85.5
Note: The same 8-bit binary code represents a number. When you think it represents a binary number and think it represents a binary decimal number, the value is different.
For example, 00011000, when it is regarded as a binary number, its value is 24, but as a two-bit BCD code, its value is 18. For example, if 00011100 is regarded as a binary number, its value is 28, but it cannot be regarded as a BCD code, because it is an illegal code in the 8421bcd code.
2. BCD codes in the BCD format of computers are often used in two formats: separating BCD codes and combining BCD codes.
The so-calledSeparate BCD codesRepresents a digit in decimal number using the lower four-bit encoding of one byte. For example, the storage format of the number 82 is: _ 1 0 0 0 _ 0 0 1 0 where _ indicates the irrelevant value.
Combined BCD codeStores two decimal digits in one byte, the storage format of example 82 is 1000 0010 BCD code addition and subtraction. Because the encoding is to represent each decimal number with a set of four binary numbers, therefore, if the BCD code is directly transferred to the computer for calculation, the result may be wrong because the computer always treats the number as a binary number. For example, the BCD code is used to calculate 38 + 49. The solution is to use "Add 6 correction" for the result of the binary addition operation, which is called BCD adjustment.
The result of the binary addition operation is corrected to the result of the BCD code addition operation. When two BCD numbers are added, the results of the binary addition operation are corrected using the correction rules.
Modify rules:
(1) if the result of adding the BCD numbers of any two corresponding BCD numbers is not carried to the first one, if the result is smaller than or equal to 9, this does not need to be corrected; if the result is greater than 9 and less than 16, this bit is corrected with 6.
(2) If the result of adding the BCD numbers of the two corresponding BCD numbers to the first one has an increment (that is, the result is greater than or equal to 16), this bit is corrected by 6.
(3) when the result of the low position correction is greater than 9, the high position is increased by 6. The following is an example to verify the correctness of the above rules. Use BCD code for 35 + 21 BCD code for 25 + 37 use BCD code for 38 + 49 use BCD code for 42 + 95 use BCD code for 91 + 83 use BCD code for 94 + 7. When the BCD Code 76 + 45 is used for subtraction, when there is a bid for a high position, the difference between "borrow for sixteen" and "borrow for ten" is 6 more than the correct result, the "6 minus correction" method can be used for correction. when the two BCD codes are added and subtracted, the bitwise addition and subtraction command is used to calculate the result, and then the BCD adjustment command is used to adjust the result to obtain the correct decimal operation result. In fact, there are both BCD number adjustment commands in the computer and BCD number separation adjustment commands.
In addition, the addition and subtraction operations of BCD codes can also be converted from the program to the binary number before the operation, and then processed by the computer on the binary number operation, after the calculation, the binary result is converted from a program to a BCD code.
II,ASCII codeThe ASCII code is the header of the American Standard Code for information interchange. Early ASCII codes used 7-bit binary code to encode characters. It contains 32 general-purpose control characters, 10 Arabic numerals, 52 English letters, and 34 special symbols, totaling 128. The 7-bit ASCII Code adds a "0" in the highest bit to form an 8-bit code, which occupies exactly one byte. In the storage and transmission information, the highest bit is often used as the parity bit.
Expanded ASCII codeThat is, the eighth digit is not regarded as the verification bit but as the encoding bit. There are 256 extended ASCII codes.
The ing between the decimal number and 8421bcd encoding is given here.