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.
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: