Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Can somebody explain this code

Status
Not open for further replies.
Advanced Member level 4
Joined
Jul 25, 2012
Messages
1,192
Helped
171
Reputation
342
Reaction score
162
Trophy points
1,343
Visit site
Activity points
0
Hello!

I need a n explanation of
Code:
 bYear = (((bYear / 10) << 4) & 0xC0) + ((bYear-(bYear / 10) * 10) & 0b00001111);
see post 12 at http://www.mikroe.com/forum/viewtopic.php?p=84795

He is using byear as a variable which increments from 0 to 99 decimal

He is then converting for eg: decimal 99 to bcd using the above statement

bYear/10 = 99/10 = 0x09 or d9 {integer division)
(bYear / 10) << 4) = 9 << 4 = 0x90 or d144
(bYear / 10) << 4) & 0xC0) = 0x90 & 0xC0 = 0x80 or d128

bYear/10 = 0x09 or d9
(bYear / 10) * 10) = d9 * 10 = d90 or 0x5A
(bYear-(bYear / 10) * 10) = d99 - d90 or 0x63 - 0x5A = d9 or 0x09
((bYear-(bYear / 10) * 10) & 0b00001111) = d9 or 0x09

(((bYear / 10) << 4) & 0xC0) + ((bYear-(bYear / 10) * 10) & 0b00001111) = d128 + d9 or 0x80 + 0x09 = d137 or 0x89

How is he writing 0x89 or d137 instead of 0x99 or dfor year 99. or how is writing d153 for year 99?

i think it should be
Code:
 bYear = (((bYear/10)<<4) + (bYear%10));

so that,
(bYear/10) = 99/10 = d9 or 0x09
((bYear/10)<<4) = 0x09 << 4 or d9 << 4 = 0x90 or d144
(bYear%10) = 99%10 = d9 or 0x09
(((bYear/10)<<4) + (bYear%10)) = 0x90 + 0x09 = 0x99 or d144 + d9 = 153

Isn't the second equation right?

Isn't this equation
Code:
 (((bYear / 10) << 4) & 0xC0) + ((bYear-(bYear / 10) * 10) & 0b00001111)
wrong for Decimal to BCD Conversion?
 
Last edited:

It should be:

Code:
bYear = (((bYear / 10) << 4) & 0xF0) + ((bYear-(bYear / 10) * 10) & 0b00001111);

AND-ing with 0xF0 and 0x0F seems unnecessary.


The one you posted is fine. This is fine:
Code:
 bYear = (((bYear/10)<<4) + (bYear%10));

Instead of adding, you can also OR:
Code:
bYear = (((bYear/10)<<4) | (bYear%10));
 

Code:
bYear = (((bYear / 10) << 4) & 0xF0) + ((bYear-(bYear / 10) * 10) & 0b00001111);
Seems like a reminiscence to programming languages that don't know a modulus operator (% in C) like original MS BASIC, or the code has been written by a programmer who din't yet learn about it.

It might be interesting to count the generated number of machine instructions compared to the C standard procedure.
Code:
 bYear = (((bYear/10)<<4) + (bYear%10));
It can be still minimized by using assembly code, built-in functions like dec2bcd() are supposed to do.
 

So, I tested this with mikroC (selected PIC16F877A as the target device). Here are the results:

Code:
bYear = (((bYear / 10) << 4) & 0xF0) + ((bYear-(bYear / 10) * 10) & 0b00001111);

308 cycles.
_____________________________________________________________________________
Code:
bYear = (((bYear/10)<<4) | (bYear%10));
Code:
bYear = (((bYear/10)<<4) + (bYear%10));

227 cycles.
_____________________________________________________________________________
Code:
bYear = DEC2BCD(bYear);

247 cycles.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top