Best search algorithm to find condition within a range

Marko Rauhamaa marko at pacujo.net
Thu Apr 9 06:00:05 EDT 2015


BartC <bc at freeuk.com>:

> On 08/04/2015 23:06, Marko Rauhamaa wrote:
>> Arithmetics is performed just like in elementary school.
>
> Not in the school I went to; we used decimal!

The basic arithmetic algorithms are independent of the base.

For example, here's how you can add two 128-bit integers in C using
64-bit digits:

    typedef struct {
        uint64_t lo, hi;
    } uint128_t;

    uint128_t add128(uint128_t x, uint128_t y)
    {
        uint128_t result = {
            .lo = x.lo + y.lo,
            .hi = x.hi + y.hi
        };
        if (result.lo < x.lo)
            result.hi++;
        return result;
    }

Works both for signed and unsigned integers.    


Marko



More information about the Python-list mailing list