This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

40 bit subtraction

Can anyone please give me some hints as to how to go about subtracting one 40 bit number from another?
At the start of a process I read a 5 byte counter from external hardware and store it. At the end of the process I read the 5 byte counter again. I need to find how much the count has incremented by regardless of whether the counter has wrapped or not.
e.g. start 0xFFFFFFFFFE, end 0x0000000001. The answer would be 3.

Parents
  • Something like the following model, perhaps? Adjust to suit your taste...

    struct _40BITS
    {
        unsigned char ms8;
        unsigned long ls32;
    };
    
    struct _40BITS sub40(struct _40BITS minuend, struct _40BITS subtrahend)
    {
        struct _40BITS difference;
    
        if (minuend.ls32 < subtrahend.ls32)
            --minuend.ms8;
    
        difference.ls32 = minuend.ls32 - subtrahend.ls32;
        difference.ms8  = minuend.ms8  - subtrahend.ms8;
        return difference;
    }
    
    int main(void)
    {
        struct _40BITS start = {0xFF,0xFFFFFFFE};
        struct _40BITS end   = {0x00,0x00000001};
        struct _40BITS diff  = sub40(end, start);
        return 0;
    }

Reply
  • Something like the following model, perhaps? Adjust to suit your taste...

    struct _40BITS
    {
        unsigned char ms8;
        unsigned long ls32;
    };
    
    struct _40BITS sub40(struct _40BITS minuend, struct _40BITS subtrahend)
    {
        struct _40BITS difference;
    
        if (minuend.ls32 < subtrahend.ls32)
            --minuend.ms8;
    
        difference.ls32 = minuend.ls32 - subtrahend.ls32;
        difference.ms8  = minuend.ms8  - subtrahend.ms8;
        return difference;
    }
    
    int main(void)
    {
        struct _40BITS start = {0xFF,0xFFFFFFFE};
        struct _40BITS end   = {0x00,0x00000001};
        struct _40BITS diff  = sub40(end, start);
        return 0;
    }

Children
No data