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.

How does this code work, can somebody explain?

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

I want to know how this BCD to Decimal code works.

Code:
 ((bcd >> 4)*10+(bcd & 0x0F));

Should I have to give 0x99 for year 99 for bcd in the formula to get 153 decimal

i.e., (bcd >> 4) = 0x99 >> 4 = 0x09 or d9
(bcd >> 4)*10 = 0x09 * d10 = 0x90 or d144
(bcd & 0x0F) = 0x99 & 0F = 0x09 or d9
finally 0x90 + 0x09 = 0x99 or d144 + d9 = d153.

I have confusion here (bcd >> 4)*10 = 0x09 * d10 = 0x90 or d144 does it calculate 0x90 * d10 or 0x90 * 0x0A? I think it calulates 0x90 * d10 because 10 is written as 10 and not 0x10 or 0x0A. Am I right?
 

Joined
Jul 25, 2012
Messages
1,192
Helped
171
Reputation
342
Reaction score
162
Trophy points
1,343
Activity points
0
No it converts to decimal 153 for bcd 0x99

----------Update--------------

bcd = 0x99

(bcd >> 4) = 0x99 >> 4 = 9

(bcd >> 4)*10 = 9 * 10 = 90

(bcd & 0x0F) = 0x99 & 0x0F = 0x09

(bcd >> 4)*10+(bcd & 0x0F) = 90 + 9 = 99

Am I right now?
 
Last edited:

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
49,408
Helped
14,439
Reputation
29,142
Reaction score
13,234
Trophy points
1,393
Location
Bochum, Germany
Activity points
284,341
I mean, use the pocket calculator to trace the operation of the rather straightforward bcd2dec() code.

I really don't understand where you expect to get "decimal 153". After some discussions, we all know the the RTC register content for year 99 is "1001 1001" binary or 0x99 or 153 decimal. Now you're converting it to decimal. We expect an output of 99 decimal, or 0x63, or "0110 0011" binary, isn't it?
 
  • Like
Reactions: tpetar

    tpetar

    Points: 2
    Helpful Answer Positive Rating
Joined
Jul 25, 2012
Messages
1,192
Helped
171
Reputation
342
Reaction score
162
Trophy points
1,343
Activity points
0
@ FvM I updated my last post, please check it.

- - - Updated - - -

@ Tahmid. Yes You are right. See my last post. Is the calculation correct?
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,792
Reputation
3,576
Reaction score
1,651
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,539
No it converts to decimal 153 for bcd 0x99

----------Update--------------

bcd = 0x99

(bcd >> 4) = 0x99 >> 4 = 9

(bcd >> 4)*10 = 9 * 10 = 90

(bcd & 0x0F) = 0x99 & 0x0F = 0x09

(bcd >> 4)*10+(bcd & 0x0F) = 90 + 9 = 99

Am I right now?

Yes, that's correct.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top