Linux Kernel in C Language -- convert BCD code to binary and binary to BCD code
Before analyzing the code, let's take a look at the differences between the BCD code and the binary code?
Those who have studied computer principles and digital electronic technology will know what they mean. Some of them have learned and passed the examination, after a while, I forget it. Today, we use a code case to say:
Let's first check Baidu to find out the BCD code:
BCD code (Binary-Coded Decimal ?) It is also called Binary Code Decimal or binary-decimal code. Use four-digit binary numbers to represent 0 ~ 9. It is a binary digital encoding format that uses binary-encoded decimal code. BCD code uses four digits to store a decimal number, so that the conversion between binary and decimal can be quickly carried out. This encoding technique is most commonly used in the design of the accounting system, because the accounting system often needs to accurately calculate a long number string. Compared to the general floating-point memory type, BCD code can be used to save the accuracy of the value, but also to avoid the time spent by the computer for floating-point operations. BCD encoding is also commonly used for other computation that requires high accuracy. Since the decimal number is 0, 1, 2 ,...... And 9 digits. Therefore, at least four binary codes are required to represent the one-digit decimal number. 4-bit binary code has a total of 2 ^ 4 = 16 types of code groups. In these 16 types of code, you can choose any 10 types to represent 10 decimal digits, a total of N = 16! /[10! * (16-10)!] Is equal to 8008 solutions. Common BCD codes are listed at the end. BCD codes can be divided into two types: 8421, 2421, and 5421 BCD codes, of which 8421 are the most commonly used; 3 or more BCD codes are not permitted, the remaining 3 round robin codes.
BCD code calculation rules: BCD code is a decimal number. When the calculator adds or subtracted data, it processes the data according to the binary calculation rules. In this way, when the BCD code is sent to the calculator for calculation, the result needs to be corrected. The correction rule is: When two BCD codes are added together, if the sum is equal to or less than 1001 (that is, 9 in decimal number), no correction is required; if the sum of the values is Between 1010 and 1111 (that is, the hexadecimal number is 0AH ~ 0 FH), you need to add 6 for correction. If the base produces carry during addition, you also need to add 6 for correction. The reason for this is that the machine is added in binary format. Therefore, when the 4-bit binary number is added, the operation is performed based on the principle of "every 16 to one, in essence, it is the addition of two decimal numbers. It should be added according to the "every ten in one" principle, and the difference between 16 and 10 is 6. So when it is 9 or more, you must add 6 for correction.
A little trembling, let's take a look at the Code:
# Include
# Include
// Convert the BCD code to binary unsigned bcd2bin (unsigned char val) {return (val & 0x0f) + (val> 4) * 10 ;} // convert binary to BCD code unsigned char bin2bcd (unsigned val) {return (val/10) <4) + val % 10;} int main (void) {unsigned val = 17; printf ("bin: % u ---> 0x % x \ n", val, val); printf ("bcd: % u ---> 0x % x \ n ", bin2bcd (val), bin2bcd (val); return 0 ;}
Running result: From the result, we can see that the number of digits 17 is 0001 0001 ----> corresponding to hexadecimal 0x11, 17 is converted into BCD code, and the four digits indicate one digit, so it is expressed as the Binary Number 0001 0111 -----> corresponding to the hexadecimal number 0x17, that is, the decimal 23
Conclusion: The BIN code is the binary BCD code. The principle is to use four bits to represent a single Bit from the low position, BCD code is the binary number in decimal format. Another BCD code is not used for binary conversion. The principle is the same.