Click here to Skip to main content
15,887,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi forum,

does anyone know of a resource covering arithmetic on aggregated types?

I'm using addition like this:
C
typedef struct
{
    uint8_t hb;
    uint8_t mb;
    uint8_t lb;
}BunchOBytes;

BunchOBytes Add(BunchOBytes in0, BuncOBytes in1)
{
    BunchOBytes result;
    uint16_t sum;

    sum = in0.lb + in1.lb;
    result.lb = sum;
    sum >>= 8;
    sum += in0.mb + in1.mb;
    result.mb = sum;
    sum >>= 8;
    sum += in0.hb + in0.hb;
    result.hb = sum;

    return(result); // handle overflow
}
Now I'm in need of division by a scalar. Can anyone recommend a book on this?
Posted
Updated 26-Nov-15 20:40pm
v2
Comments
Philippe Mori 26-Nov-15 12:20pm    
In addition to Solution 1, that code does not make much sense as an unsigned is typically 32 bits and your addition works on 24 bits...
lukeer 27-Nov-15 2:42am    
Corrected that. I forgot to mention that this is not for .NET or even PC but an embedded device. I changed the data type within the struct to clearly state what size it is.
CPallini 27-Nov-15 2:59am    
What is your requirement, exactly? Doesn't your system provide, for instance, the division operation on 24 (possibly 32) bit integers? Depending on your needs, you might also find assembly better suited for this.
lukeer 30-Nov-15 3:20am    
The requirement has gone academic during wednesday afternoon (and, in fact, evening) due to some changes to pre- and post-scaler settings. With this new setup, 24 bits are indeed enough and the compiler takes care of all the fiddling with 8 bit registers.

But who knows? Some time an 8-bit device will have to calculate on something larger than 32 bits. So I'm still interrested in algorithms adding and multiplying arbitrary amounts of chars to one another.

1 solution

It seems that you don't want to use an enum but a structure:
C++
typedef struct
{
    unsigned hb;
    unsigned mb;
    unsigned lb;
}BunchOBytes;
 
Share this answer
 
Comments
lukeer 27-Nov-15 2:43am    
Yes, of course. Thanks for pointing that out and have a 5.
Any hint on the actual question?
Jochen Arndt 27-Nov-15 3:43am    
You may have a look at this link: http://www.bearcave.com/software/divide.htm
I have done similar so far only using assembly on Microchip processors. For those and other processors there are libraries with sources.

If your C compiler supports 32-bit integers and speed is not too important, I would just let the compiler do the work as already suggested by CPallini.
lukeer 30-Nov-15 3:24am    
Thank you, that's exactly the type of stuff I was looking for.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900