# Thread: How to divide 2 byte in C ?

1. ## 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.
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 ?

Thanks.

2. Originally Posted by Josh_mt
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.
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.
I'm thinking that's not correct. Any residual value can be added right to your R0, without being multiplied by anything.

Say the number was 350. You divide R1 by 58, and have 6 with a remainder of 2. You add the two onto R0. If R0 should ever exceed 255, then sure, the R1 value needs to be adjusted upward.

As far as an algorithm, I was thinking of bit shifting right >> , (dividing by two), looping until the amount that you had shifted could not be done any more and stay under the amount of the divisor. Then subtracting the remainder (if the divisor wasn't a power of 2).

It's too late to show you an example, but hopefully you'll get the spark of a good idea on your own.

I'm not very experienced with bit-shifting, but I believe this is correct. It is very late however. (pre excuse if it's a wacko idea! <lol> )

And Welcome to the forum, Josh_mt!

3. ## 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.

4. Since you're programming in C, why don't you just create a 16-bit variable (say an unsigned short int) with R1x256 + R0 and then divide that by 58?

But what I understand is because R1 is the high byte, any residual results from the division should be multiplied by 256, because it's the high byte.

Is it right ?

Anyway, thank you for your help

6. ## 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 ?

7. That doesn't make any sense. How can "my_data" be a 16 bit value if you don't have any 16 bit values in your microcontroller?

Quzah.

8. Actually the range of the my_data is from 0 until 18500 in decimal.
So, if I want to convert it to binary it will be 2 bytes.
And because my microcontroller doesn't have any place with 16 bit size, I divide it to 2 separate register, each of them hold the high byte (R1) and low byte (R0).

And now I don't know how to divide it with 58.

9. That's what the C compiler does for you. It generates the necessary code to "fake" a 16-bit integer out of two 8-bit locations.

Otherwise, I suggest you google "binary long division" if you want to roll your own code.

10. How do you think people divide two 64-bit numbers on a 32-bit PC?
Answer: You just declare variables of the desired size e.g. long long, and then do a = b / c;
The compiler works out how to do it using 32-bit registers for you.
The same deal goes for doing 32-bit divides on 16-bit hardware, or 16-bit divides on 8-bit hardware.

If you're writing it in C, then just write C code. You don't have to care about registers.

11. ## 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.
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

Popular pages Recent additions