Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

Can somebody explain this code

Status
Not open for further replies.
Joined
Jul 25, 2012
Messages
1,192
Helped
170
Reputation
340
Reaction score
161
Trophy points
1,343
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:

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
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));
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
47,841
Helped
14,116
Reputation
28,489
Reaction score
12,797
Trophy points
1,393
Location
Bochum, Germany
Activity points
277,694
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.
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top