How to divide 2 byte in C ?

This is a discussion on How to divide 2 byte in C ? within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm now writing a program for microcontroller AT89S51, and I'm using C language. I have a 2 byte ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Talking 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 ?

    Please help me, any help would be appreciated.

    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Josh_mt View Post
    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!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    6

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

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Thanks Adak

    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

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Smile 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 ?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Smile

    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.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Smile 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
    Last edited by Josh_mt; 11-29-2010 at 09:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 08-30-2010, 09:26 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 04:19 AM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 04:46 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21