This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Explanation

Hello

Below are the functions to convert a 3 digit decimal to BCD format and to convert from 3 digit BCD to Binary. Can anyone please explain it clearly??


/************************************************************************/
/*      Name : WORDToBCD3 to Converts a 3 digit decimal to BCD format        */
*************************************************************************/

u16 WORDToBCD3(u16 value)
{
u16 bcdhigh = 0;
while (value >= 100)
{
bcdhigh++;
value -= 100;
}
bcdhigh <<= 4;
while (value >= 10)
{
bcdhigh++;
value -= 10;
}
return (bcdhigh << 4) | value;
}
/************************************************************************/
/*      Name : BCD3ToWORD to convert from 3 digit BCD to Binary        */
*************************************************************************/
u16 BCD3ToWORD(u16 value)
{
return (u16)((((value&0xF00)>>8)*100) + (((value&0x0F0)>>4)*10) +
(value&0x0F));
}

Parents
  • so you have the value 102.
    While it is >= 100, the code should increment (add 1) to the BCD number, and strip 100 from the number.

    So 102 -> 2.
    And BCD number 0->1 (binary 1)

    Then it's time to try the "ten" digit.
    <<= 4 means shift left four steps.
    So the BCD number 1 * 16 = 16 (binary 10000).

    Next repeat while the number is >= 10.
    2 < 10, so nothing to do.

    Then it's time to processed the "one" digit.
    <<= 4 once again means shift left four steps.
    So the BCD number 16 * 16 (binary 10000 << 4) = 256 (binary 100000000).

    Now the code could have iterated while the reminder of the number was larger than zero. But no need to.

    The reminder is 2 - binary 10.

    So combine the already accumulated BCD number (256 dec or 100000000 bin) with the value 2 dec or 10 bin.

    256 bit-or 2 is same as 100000000 bin bit-or 10 which is 100000010 which is 258.

    258 decimal (or 100000010 binary) is the same as 102 hexa-decimal. So now we have the same digits but in a hexadecimal number, as we originally had in a decimal number. That's exactly what we would have expected when converting the number into BCD format, i.e. binary coded decimal - one decimal digit stored per nibble (half-byte).

Reply
  • so you have the value 102.
    While it is >= 100, the code should increment (add 1) to the BCD number, and strip 100 from the number.

    So 102 -> 2.
    And BCD number 0->1 (binary 1)

    Then it's time to try the "ten" digit.
    <<= 4 means shift left four steps.
    So the BCD number 1 * 16 = 16 (binary 10000).

    Next repeat while the number is >= 10.
    2 < 10, so nothing to do.

    Then it's time to processed the "one" digit.
    <<= 4 once again means shift left four steps.
    So the BCD number 16 * 16 (binary 10000 << 4) = 256 (binary 100000000).

    Now the code could have iterated while the reminder of the number was larger than zero. But no need to.

    The reminder is 2 - binary 10.

    So combine the already accumulated BCD number (256 dec or 100000000 bin) with the value 2 dec or 10 bin.

    256 bit-or 2 is same as 100000000 bin bit-or 10 which is 100000010 which is 258.

    258 decimal (or 100000010 binary) is the same as 102 hexa-decimal. So now we have the same digits but in a hexadecimal number, as we originally had in a decimal number. That's exactly what we would have expected when converting the number into BCD format, i.e. binary coded decimal - one decimal digit stored per nibble (half-byte).

Children