BCD code converted to decimal or decimal to BCD code

Source: Internet
Author: User

The BCD code is actually the number in the digital circuit that was previously said to represent a number in a 0-9 with a 4-bit binary value.
Example: 0000=0 0001=1 0010=2 0011=3
This means that if a number is used as a BCD code, for example: 11 22
Then he should expand to: 00010001 00100010
That corresponds to decimal: 17 34
So a BCD? The idea of turning into decimal data is to separate the BCD code from each number, such as the 11 above
Separate the 10-bit 1 and move the 4-bit right by the previous 10-digit number. Decimal to BCD code
Thought: This decimal number is expressed in binary notation, the number of high 4 digits is how many * 10 plus the lower four bits represent
The numbers, C programs are as follows:

#define BCD_TO_DECIMAL (BCD) (((BCD) >> 4) + (BCD)% 10);
#define DECIMAL_TO_BCD (int dec) ((0xf0 & (Dec) >> 4) *10 + (0x0f & (DEC)))

BCD code converted to decimal or decimal to BCD code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.