Adding 64 bit numbers using 32 bit arithmetic

前端 未结 6 889
渐次进展
渐次进展 2020-12-05 06:02

How do we add two 64 bit numbers using 32 bit arithmetic??

6条回答
  •  鱼传尺愫
    2020-12-05 06:09

    The C code previously posted is unnecessarily verbose:

    unsigned a1, b1, a2, b2, c1, c2;
    
    c1 = a1 + b1;
    c2 = a2 + b2;
    
    if (c1 < a1)
        c2++;
    

    The 'a1' in the 'if' can be replaced with 'b1' as well. On overflow, c1 will be less than both a1 and b1.

提交回复
热议问题