How to divide 2 byte in C ?
Hey guys,
I'm now writing a program for microcontroller AT89S51, and I'm using C language.
I have a 2 byte data which is located in R1 and R0, where R1 is the high byte and R0 is the low byte.
To describe more : my_data = R1x256 + R0
Now I have to divide my data by 58, and I have no idea how to do it. :confused:
If I divide R1 with 58, then I will get a residue. I cannot move the residue to R0, because if I want to do it then I have to multiply the residue with 256. which will make an overflow to R0.
Do you have any algorithm to do this ?
Please help me, any help would be appreciated.
Thanks.
Re : How to divide 2 byte in C ?
Hey, thanks Quzah.
After I read the link I think I should do bit shifting to the right, but what I understand is it can only divide my data with a number which is a power of 2 ( i.e. 2, 4, 8, 16, etc)
I just try to shift the data 6 times to the right, which actually divide it by 2^6 = 64 ( the nearest number to 58 ), but the result is too far with the actual result.
Is there any other solution ?
Thanks for your help.
Re : How to divide 2 byte in C ?
No, I can't Salem, because I will download the program into microcontroller, so my microcontroller will be the one that execute the program.
The problem is my microcontroller doesn't have any place for 16 bit data, so it must be divided to 2 separate byte.
Any help please ?
Re : How to divide 2 byte in C ?
Ahh, okay now I understand.
I just try with this "fake" 16 bit variable and it works. :D
I have also tried to disassembly the C code, and now I also understand how to do this in assembly language ( it is so complicated, better in C ;) )
Sorry I didn't expect that this might work.
Thanks everyone :D