Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

help with BCD to INT

Status
Not open for further replies.

skmdmasud

Member
I have the following code which in line 2 reads a RTC in Temp as BCD
then in line 3 it should convert the BCD to int and puts it in min variable.

1. /Read the Minute Register
2. DS1307Read(0×01,&temp);
3. min=(((temp & 0b01110000)>>4)*10)+(temp & 0b00001111);

I dont understand how the conversion is working. let say temp = 0011 0101 (3 and 5 in decimal)

so when we '&' it with 0111 0000 we get
0111 0000
0011 0101
------ ------- '&'
0011 0000
shift it 4 places '>>4' = 0000 0011

then multiply with 10 dec,
00000011
00001010 (10 dec)
------------------------- multiply
00011110


in the same way second part
0011 0101
0000 1111
-------------- '&'
0000 0101


after that we add the 2 parts

00011110
00000101
--------------- addition
0010 0011 decimal for this is 35


So, first of all how did it become decimal as we can see till the end its binary. It just made the binary coded decimal to 8 bit binary

Regards.
 
Last edited:
Its quite simple if you look at the equation

Mask off the top nibble to leave 48 ( 0x30) divide by 16 ( shift >> 4) (0x3) multiply by 10 (30 ) mask off the bottom nibble and add to result (5)

(48 / 16) * 10 = 30 + 5 = 35
 
I totally agree with you. We understand that the 5th and 6th place are 16 and 32 and so on... What i dont understand how is the binary becoming decimal in term of programming language? all our inputs are in binary
 
I don't think I'm following you..... Decimal and Binary are one and the same... Its the BCD that the odd one out. Don't confuse BCD as ANY kind of number.. Its a hybrid...

Take the number 52 its 0x34 in hexadecimal... And 00110100 in binary... but its 0x52 in BCD.. This is because RTC devices have decade counters in side...
 
I don't think I'm following you..... Decimal and Binary are one and the same... Its the BCD that the odd one out. Don't confuse BCD as ANY kind of number.. Its a hybrid...

Take the number 52 its 0x34 in hexadecimal... And 00110100 in binary... but its 0x52 in BCD.. This is because RTC devices have decade counters in side...

Ok i understand now... decimal and binary are same C will do the binary to int and int to binary itself we just have to pass the binary or int in the variable.

Like we can use a int variable as binary or decimal storage.
 
Status
Not open for further replies.
Back
Top