<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Explanation</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/28675/explanation</link><description> 
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 :</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Explanation</title><link>https://community.arm.com/thread/136673?ContentTypeID=1</link><pubDate>Sun, 04 Dec 2011 07:06:00 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:7ae396d9-4c12-46dc-9ac1-1ebd3c5cb158</guid><dc:creator>Matt Werner</dc:creator><description>&lt;p&gt;&lt;p&gt;
Thank you all.. especially Westermark reply helped me a lot to
undertand it very clearly..&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Explanation</title><link>https://community.arm.com/thread/130918?ContentTypeID=1</link><pubDate>Fri, 02 Dec 2011 00:40:44 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:df4b532e-7fd1-42e3-a2c4-38f156fa833c</guid><dc:creator>ntino gr</dc:creator><description>&lt;p&gt;&lt;p&gt;
&amp;gt;&lt;i&gt;binary coded decimal - one decimal digit stored per nibble
(half-byte).&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Should be referenced as&lt;br /&gt;
&lt;b&gt;BCD&lt;/b&gt; numbers normally occupy one byte per decimal digit.&lt;br /&gt;
&lt;b&gt;packed BCD&lt;/b&gt; numbers use half byte or nibble for a decimal digit
and in a byte are stored two decimal digits.&lt;/p&gt;

&lt;p&gt;
&lt;a href="http://en.wikipedia.org/wiki/Binary-coded_decimal"&gt;en.wikipedia.org/.../Binary-coded_decimal&lt;/a&gt;&lt;br /&gt;


&lt;a href="http://academic.evergreen.edu/projects/biophysics/technotes/program/bcd.htm"&gt;academic.evergreen.edu/.../bcd.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
This process of representation of a value to the binary system is
called encoding.&lt;br /&gt;
So, the actual value of a byte(8bits) or a word(16bits) varies along
with the encoding used to store the information.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Explanation</title><link>https://community.arm.com/thread/127247?ContentTypeID=1</link><pubDate>Thu, 01 Dec 2011 18:13:49 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ec87f9e8-7b7c-4925-aeb0-55511e26bd20</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
so you have the value 102.&lt;br /&gt;
While it is &amp;gt;= 100, the code should increment (add 1) to the BCD
number, and strip 100 from the number.&lt;/p&gt;

&lt;p&gt;
So 102 -&amp;gt; 2.&lt;br /&gt;
And BCD number 0-&amp;gt;1 (binary 1)&lt;/p&gt;

&lt;p&gt;
Then it&amp;#39;s time to try the &amp;quot;ten&amp;quot; digit.&lt;br /&gt;
&amp;lt;&amp;lt;= 4 means shift left four steps.&lt;br /&gt;
So the BCD number 1 * 16 = 16 (binary 10000).&lt;/p&gt;

&lt;p&gt;
Next repeat while the number is &amp;gt;= 10.&lt;br /&gt;
2 &amp;lt; 10, so nothing to do.&lt;/p&gt;

&lt;p&gt;
Then it&amp;#39;s time to processed the &amp;quot;one&amp;quot; digit.&lt;br /&gt;
&amp;lt;&amp;lt;= 4 once again means shift left four steps.&lt;br /&gt;
So the BCD number 16 * 16 (binary 10000 &amp;lt;&amp;lt; 4) = 256 (binary
100000000).&lt;/p&gt;

&lt;p&gt;
Now the code could have iterated while the reminder of the number
was larger than zero. But no need to.&lt;/p&gt;

&lt;p&gt;
The reminder is 2 - binary 10.&lt;/p&gt;

&lt;p&gt;
So combine the already accumulated BCD number (256 dec or
100000000 bin) with the value 2 dec or 10 bin.&lt;/p&gt;

&lt;p&gt;
256 bit-or 2 is same as 100000000 bin bit-or 10 which is 100000010
which is 258.&lt;/p&gt;

&lt;p&gt;
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&amp;#39;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).&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Explanation</title><link>https://community.arm.com/thread/136672?ContentTypeID=1</link><pubDate>Thu, 01 Dec 2011 14:50:42 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c1f8195c-1df2-483c-88a6-4a97b15febf5</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
Some suggestions here: &lt;a href="http://blog.antronics.co.uk/2011/08/08/"&gt;blog.antronics.co.uk/.../&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Explanation</title><link>https://community.arm.com/thread/127254?ContentTypeID=1</link><pubDate>Thu, 01 Dec 2011 12:00:39 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b5b5e7a0-844d-41fc-8422-0bb1a8ceb34d</guid><dc:creator>erik malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;Am i wrong somewhere???&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
yes, get a &amp;#39;C&amp;#39; book and study it.&lt;/p&gt;

&lt;p&gt;
This is about as elementary as it can get&lt;/p&gt;

&lt;p&gt;
Erik&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Explanation</title><link>https://community.arm.com/thread/116614?ContentTypeID=1</link><pubDate>Thu, 01 Dec 2011 11:46:22 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:74727b38-5f0b-4eec-b3f2-5bbd7f557685</guid><dc:creator>Matt Werner</dc:creator><description>&lt;p&gt;&lt;p&gt;
Ok.. i did a small study on th eoperators and here is what i
understood. I also tried to do an example s.. see bottom... But i am
getting wrong result. Can anyone help??&lt;/p&gt;

&lt;pre&gt;
u16 WORDToBCD3(u16 value)
{
u16 bcdhigh = 0;// initializing bcdhigh as zero
while (value &amp;gt;= 100) //Checking if it is a 3 digit number
{
bcdhigh++;//If it is a 3 digit number , then value of bcdhigh is incremented i.e now it is = 1
value -= 100; // The variable value has now new content which is value - 100
}
bcdhigh &amp;lt;&amp;lt;= 4; // means Bitwise left shift assignment. It shift bcdhigh left by 4 bits

while (value &amp;gt;= 10) // Checking if it is a 2 digit number
{
bcdhigh++;//value of bcdhigh is incremented
value -= 10;// means value = value - 10
}
return (bcdhigh &amp;lt;&amp;lt; 4) | value; // it returns the value  of (Bitwise left shift i.e all bits in bcdhigh

shifted left by 4 bits) OR each bit in value
}

&lt;/pre&gt;

&lt;p&gt;
Example lets take value = 102&lt;br /&gt;
then while (102 &amp;gt;= 100)&lt;br /&gt;
bcdhigh= 1 ( in binary 1)&lt;br /&gt;
value = 102 -100 i.e 2 ( in binary 0010)&lt;br /&gt;
bcdhigh &amp;lt;&amp;lt;= 4; means 10000&lt;/p&gt;

&lt;p&gt;
returning value = 10000 OR 0010 which gives a binary value of
0b10011100011000 ( decimal equivalent = 10008) ????&lt;/p&gt;

&lt;p&gt;
Am i wrong somewhere???&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Explanation</title><link>https://community.arm.com/thread/105543?ContentTypeID=1</link><pubDate>Thu, 01 Dec 2011 10:41:55 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8c30a9d8-f9ed-4527-b43a-3c4797ce03ed</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
&amp;quot;using &amp;#39;/&amp;#39; and &amp;#39;%&amp;#39; it could be much more efficient.&amp;quot;&lt;/p&gt;

&lt;p&gt;
Not always. It depends on processor support for integer division -
still lacking in some new processors.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Explanation</title><link>https://community.arm.com/thread/79804?ContentTypeID=1</link><pubDate>Thu, 01 Dec 2011 10:17:55 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0d5501f1-8eb7-4aef-b308-3fbd86383bde</guid><dc:creator>erik malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
using &amp;#39;/&amp;#39; and &amp;#39;%&amp;#39; it could be much more efficient.&lt;/p&gt;

&lt;p&gt;
anyhow as to the OP&amp;#39;s question, I totally agree with Al&lt;/p&gt;

&lt;p&gt;
Erik&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Explanation</title><link>https://community.arm.com/thread/60140?ContentTypeID=1</link><pubDate>Thu, 01 Dec 2011 09:48:06 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:3408c7df-d631-4afe-8eea-e801326e3546</guid><dc:creator>doubt that my ISP Al Bradford</dc:creator><description>&lt;p&gt;&lt;p&gt;
Mathew;&lt;/p&gt;

&lt;p&gt;
The code appears straight forward enough. If you don&amp;#39;t understand
the shift operations and compare operations, I suggest that you look
up these operations in a good C manual.&lt;/p&gt;

&lt;p&gt;
Also, I suggest that you code this in a simple program and step
through the code and monitor each operation and var for a full
understanding.&lt;/p&gt;

&lt;p&gt;
Bradford&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>