Skip to content

IntToU64

Description

Convert an integer to u64.

This public macro supports both forms:

  • IntToU64(value) - returns the result, no error channel.
  • IntToU64(value, error) - writes the error flag through error.

Parameters

Name Direction Description
value in Integer to convert.
error out Optional pointer set to true on failure and false on success.

Usage example (from documentation)

  u64 v = IntToU64(&big);

Success

Returns the numeric value as a u64. The integer is not modified.

Failure

Returns 0 when the value does not fit in 64 bits. With the two-argument form *error is set to true; with the one-argument form the caller cannot distinguish overflow from a true zero result.

Usage example (Cross-references)

Usage examples (Cross-references)
                return false;
            }
            rem  = IntToU64(&remainder);
            last = IntIsZero(&quotient);
        }
    
        return int_pow_u64(result, base, IntToU64(exponent));
    }
            return 0;
        }
        rem = IntToU64(&remainder);
    
        IntDeinit(&divisor_value);
    
        bool result = IntEQ(&clone, &original);
        result      = result && (IntToU64(&clone) == 11);
    
        IntShiftLeft(&original, 1);
    
        result = result && !IntEQ(&clone, &original);
        result = result && (IntToU64(&clone) == 11);
        result = result && (IntToU64(&original) == 22);
        result = result && !IntEQ(&clone, &original);
        result = result && (IntToU64(&clone) == 11);
        result = result && (IntToU64(&original) == 22);
    
        IntDeinit(&original);
        IntAdd(&sum, &a, &b);
    
        bool result = (IntToU64(&sum) == 256);
        result      = result && (IntBitLength(&sum) == 9);
    
        bool result = IntBitLength(&v255) == 8;
        result      = result && (IntToU64(&v255) == 255);
    
        IntDeinit(&v255);
        IntShiftLeft(&value, 4);
    
        bool result = IntToU64(&value) == 48;
        result      = result && (IntBitLength(&value) == 6);
        IntShiftRight(&value, 4);
    
        bool result = IntToU64(&value) == 3;
        result      = result && (IntBitLength(&value) == 2);
        text = IntToBinary(&result_value);
    
        bool result = IntToU64(&result_value) == 256;
        result      = result && (ZstrCompare(StrBegin(&text), "100000000") == 0);
    
        IntAdd(&result_value, &base, &rhs);
        bool result = IntToU64(&result_value) == 42;
    
        IntAdd(&result_value, &base, 2);
    
        IntAdd(&result_value, &base, 2);
        result = result && (IntToU64(&result_value) == 42);
    
        IntAdd(&result_value, &base, -2);
    
        IntAdd(&result_value, &base, -2);
        result = result && (IntToU64(&result_value) == 38);
    
        IntAdd(&result_value, &huge, 10);
    
        bool result = IntSub(&result_value, &a, &b);
        result      = result && (IntToU64(&result_value) == 255);
    
        IntDeinit(&a);
    
        bool result = IntSub(&result_value, &base, &rhs);
        result      = result && (IntToU64(&result_value) == 38);
    
        result = result && IntSub(&result_value, &base, 2u);
    
        result = result && IntSub(&result_value, &base, 2u);
        result = result && (IntToU64(&result_value) == 38);
    
        result = result && IntSub(&result_value, &base, -2);
    
        result = result && IntSub(&result_value, &base, -2);
        result = result && (IntToU64(&result_value) == 42);
    
        result = result && IntSub(&result_value, &huge, 90);
    
        result = result && !IntSub(&preserved, &base, 50);
        result = result && (IntToU64(&preserved) == 99);
    
        IntDeinit(&base);
    
        bool result = !IntSub(&result_value, &a, &b);
        result      = result && (IntToU64(&result_value) == 99);
    
        IntDeinit(&a);
        IntMul(&result_value, &a, &b);
    
        bool result = IntToU64(&result_value) == 126;
    
        IntDeinit(&a);
    
        bool result = IntIsZero(&result_value);
        result      = result && (IntToU64(&result_value) == 0);
    
        IntDeinit(&a);
        IntSquare(&result_value, &value);
    
        bool result = IntToU64(&result_value) == 152399025;
    
        IntDeinit(&value);
    
        bool result = ZstrCompare(StrBegin(&qtext), "127275040218913071") == 0;
        result      = result && (IntToU64(&remainder) == 3);
    
        StrDeinit(&qtext);
        IntDiv(&result_value, &dividend, 10u);
    
        bool result = IntToU64(&result_value) == 12;
    
        IntDeinit(&dividend);
    
        bool result = !IntDivExact(&result_value, &dividend, &divisor);
        result      = result && (IntToU64(&result_value) == 99);
    
        IntDeinit(&dividend);
    
        bool result = ZstrCompare(StrBegin(&text), "127275040218913071") == 0;
        result      = result && (IntToU64(&remainder) == 3);
    
        IntDeinit(&dividend);
        IntMod(&result_value, &dividend, 10u);
    
        bool result = IntToU64(&result_value) == 6;
    
        IntDeinit(&dividend);
    
        IntDeinit(&value);
        bool result = IntToU64(&remainder) == 3;
        IntDeinit(&remainder);
        DefaultAllocatorDeinit(&alloc);
        IntGCD(&result_value, &a, &b);
    
        bool result = IntToU64(&result_value) == 6;
    
        IntDeinit(&a);
        IntLCM(&result_value, &a, &b);
    
        bool result = IntToU64(&result_value) == 42;
    
        IntDeinit(&a);
        IntRoot(&result_value, &value, 4);
    
        bool result = IntToU64(&result_value) == 8;
    
        IntDeinit(&value);
        IntRootRem(&root, &remainder, &value, 3);
    
        bool result = IntToU64(&root) == 5;
        result      = result && (IntToU64(&remainder) == 75);
    
        bool result = IntToU64(&root) == 5;
        result      = result && (IntToU64(&remainder) == 75);
    
        IntDeinit(&value);
        IntSqrt(&result_value, &value);
    
        bool result = IntToU64(&result_value) == 14;
    
        IntDeinit(&value);
        IntSqrtRem(&root, &remainder, &value);
    
        bool result = IntToU64(&root) == 14;
        result      = result && (IntToU64(&remainder) == 4);
    
        bool result = IntToU64(&root) == 14;
        result      = result && (IntToU64(&remainder) == 4);
    
        IntDeinit(&value);
        IntSquareMod(&result_value, &value, &mod);
    
        bool result = IntToU64(&result_value) == 94;
    
        IntDeinit(&value);
        IntModAdd(&result_value, &a, &b, &m);
    
        bool result = IntToU64(&result_value) == 12;
    
        IntDeinit(&a);
        IntModSub(&result_value, &a, &b, &m);
    
        bool result = IntToU64(&result_value) == 9;
    
        IntDeinit(&a);
        IntModMul(&result_value, &a, &b, &m);
    
        bool result = IntToU64(&result_value) == 22;
    
        IntDeinit(&a);
    
        bool result = IntModDiv(&result_value, &a, &b, &m);
        result      = result && (IntToU64(&result_value) == 12);
    
        IntModMul(&check, &result_value, &b, &m);
        IntPowMod(&result_value, &base, 20u, &mod);
    
        bool result = IntToU64(&result_value) == 3;
    
        IntDeinit(&base);
        IntPowMod(&result_value, &base, &exp, &mod);
    
        bool result = IntToU64(&result_value) == 445;
    
        IntDeinit(&base);
    
        bool result = IntModInv(&result_value, &value, &mod);
        result      = result && (IntToU64(&result_value) == 4);
    
        IntModMul(&check, &value, &result_value, &mod);
    
        IntModMul(&check, &value, &result_value, &mod);
        result = result && (IntToU64(&check) == 1);
    
        IntDeinit(&value);
    
        bool result = !IntModInv(&result_value, &value, &mod);
        result      = result && (IntToU64(&result_value) == 99);
    
        IntDeinit(&value);
        bool ok = IntMul(&product, &a, &b);
    
        bool result = ok && (IntToU64(&product) == (u64)121932631112635269ull);
    
        IntDeinit(&a);
    
        bool result = ok && IntIsZero(&product);
        result      = result && (IntToU64(&product) == 0u);
        result      = result && (IntBitLength(&product) == 0u);
        // exponent 0 -> 1 (loop body never runs)
        IntPow(&result, &base, 0u);
        bool ok = IntToU64(&result) == 1;
    
        // exponent 1 -> base (one odd bit, no squaring)
        // exponent 1 -> base (one odd bit, no squaring)
        IntPow(&result, &base, 1u);
        ok = ok && (IntToU64(&result) == 7);
    
        // exponent 2 -> 49 (squaring path)
        // exponent 2 -> 49 (squaring path)
        IntPow(&result, &base, 2u);
        ok = ok && (IntToU64(&result) == 49);
    
        // exponent 5 -> 16807 (mixed odd/even bits)
        // exponent 5 -> 16807 (mixed odd/even bits)
        IntPow(&result, &base, 5u);
        ok = ok && (IntToU64(&result) == 16807);
    
        IntDeinit(&base);
    
        bool ok = IntSub(&result_value, &a, &b);
        ok      = ok && (IntToU64(&result_value) == 3);
    
        IntDeinit(&a);
        Str bits = IntToBinary(&sum);
    
        bool result = (IntToU64(&sum) == 128u);
        result      = result && (ZstrCompare(StrBegin(&bits), "10000000") == 0);
        bool ok     = IntRootRem(&root, &remainder, &value, 2);
        bool result = ok;
        result      = result && (IntToU64(&root) == 100);
        result      = result && (IntToU64(&remainder) == 0);
        bool result = ok;
        result      = result && (IntToU64(&root) == 100);
        result      = result && (IntToU64(&remainder) == 0);
    
        IntDeinit(&value);
        bool ok     = IntRootRem(&root, &remainder, &value, 3);
        bool result = ok;
        result      = result && (IntToU64(&root) == 10);
        result      = result && (IntToU64(&remainder) == 0);
        bool result = ok;
        result      = result && (IntToU64(&root) == 10);
        result      = result && (IntToU64(&remainder) == 0);
    
        IntDeinit(&value);
        bool ok     = IntRootRem(&root, &remainder, &value, 3);
        bool result = ok;
        result      = result && (IntToU64(&root) == 10);
        result      = result && (IntToU64(&remainder) == 1);
        bool result = ok;
        result      = result && (IntToU64(&root) == 10);
        result      = result && (IntToU64(&remainder) == 1);
    
        IntDeinit(&value);
        bool ok     = IntRootRem(&root, &remainder, &value, 3);
        bool result = ok;
        result      = result && (IntToU64(&root) == 9);
        result      = result && (IntToU64(&remainder) == 270);
        bool result = ok;
        result      = result && (IntToU64(&root) == 9);
        result      = result && (IntToU64(&remainder) == 270);
    
        IntDeinit(&value);
        bool ok     = IntRootRem(&root, &remainder, &value, 4);
        bool result = ok;
        result      = result && (IntToU64(&root) == 31);
        result      = result && (IntToU64(&remainder) == 76479);
        bool result = ok;
        result      = result && (IntToU64(&root) == 31);
        result      = result && (IntToU64(&remainder) == 76479);
    
        IntDeinit(&value);
        bool ok     = IntPow(&power, &base, &exp);
        bool result = ok;
        result      = result && (IntToU64(&power) == 16807u);
    
        IntDeinit(&base);
        bool ok     = IntDiv(&quotient, &dividend, (i64)7);
        bool result = ok;
        result      = result && (IntToU64(&quotient) == 142u);
    
        IntDeinit(&dividend);
        bool ok     = IntDivExact(&quotient, &dividend, (i64)7);
        bool result = ok;
        result      = result && (IntToU64(&quotient) == 143u);
    
        IntDeinit(&dividend);
        /* Signed literal forces int_mul_i64 dispatch. */
        bool ok     = IntMul(&result_value, &value, (i64)13);
        bool result = ok && (IntToU64(&result_value) == 91);
    
        IntDeinit(&value);
    
        bool ok     = IntMul(&result_value, &value, (i64)0);
        bool result = ok && (IntToU64(&result_value) == 0);
    
        IntDeinit(&value);
    
        bool ok     = IntPow(&result_value, &base, (i64)4);
        bool result = ok && (IntToU64(&result_value) == 81);
    
        IntDeinit(&base);
    
        bool ok     = IntPow(&result_value, &base, (i64)0);
        bool result = ok && (IntToU64(&result_value) == 1);
    
        IntDeinit(&base);
        /* Unsigned literal forces int_div_u64 dispatch. */
        bool ok     = IntDiv(&result_value, &dividend, 7u);
        bool result = ok && (IntToU64(&result_value) == 14);
    
        IntDeinit(&dividend);
    
        bool result = ok;
        result      = result && (IntToU64(&result_value) == 41152263u);
    
        IntDeinit(&dividend);
        bool result = ok;
        result      = result && (ZstrCompare(StrBegin(&qtext), "127275040218913071") == 0);
        result      = result && (IntToU64(&remainder) == 3u);
    
        StrDeinit(&qtext);
        bool result = ok;
        result      = result && (ZstrCompare(StrBegin(&qtext), "949667607787274453") == 0);
        result      = result && (IntToU64(&remainder) == 1u);
    
        StrDeinit(&qtext);
    
        bool result = ok;
        result      = result && (IntToU64(&result_value) == 1u);
    
        IntDeinit(&dividend);
    
        bool result = ok;
        result      = result && (IntToU64(&result_value) == 889009735u);
    
        IntDeinit(&base);
    
        bool result = ok;
        result      = result && (IntToU64(&result_value) == 1u);
    
        IntDeinit(&base);
    
        bool result = ok;
        result      = result && (IntToU64(&result_value) == 8u);
    
        IntDeinit(&value);
    
        bool ok      = IntMod(&result, &dividend, (unsigned long)7u);
        bool correct = ok && (IntToU64(&result) == (u64)2u);
    
        IntDeinit(&result);
        bool pass = ok;
        /* 3 * 4 = 12 == 1 (mod 11), so the inverse is 4. */
        pass = pass && (IntToU64(&result) == 4);
    
        IntModMul(&check, &value, &result, &mod);
    
        IntModMul(&check, &value, &result, &mod);
        pass = pass && (IntToU64(&check) == 1);
    
        IntDeinit(&value);
    
        bool pass = ok;
        pass      = pass && (IntToU64(&result) == 9);
    
        IntModMul(&check, &value, &result, &mod);
    
        IntModMul(&check, &value, &result, &mod);
        pass = pass && (IntToU64(&check) == 1);
    
        IntDeinit(&value);
    
        bool ok     = IntSqrtRem(&root, &remainder, &value);
        bool result = ok && (IntToU64(&root) == 33554432u); // 2^25
        result      = result && (IntToU64(&remainder) == 0);
        bool ok     = IntSqrtRem(&root, &remainder, &value);
        bool result = ok && (IntToU64(&root) == 33554432u); // 2^25
        result      = result && (IntToU64(&remainder) == 0);
    
        IntDeinit(&value);
    
        bool result = ok;
        result      = result && (IntToU64(&quotient) == 10309278u);
        result      = result && (IntToU64(&remainder) == 41u);
        bool result = ok;
        result      = result && (IntToU64(&quotient) == 10309278u);
        result      = result && (IntToU64(&remainder) == 41u);
    
        IntDeinit(&dividend);
    
        bool result = ok;
        result      = result && (IntToU64(&quotient) == 123456789u);
        result      = result && IntIsZero(&remainder);
        bool result = ok;
        result      = result && IntIsZero(&quotient);
        result      = result && (IntToU64(&remainder) == 42u);
    
        IntDeinit(&dividend);
    
        bool ok = int_pow_mod(&result_value, &base, &exp, &mod);
        ok      = ok && (IntToU64(&result_value) == 444);
    
        IntDeinit(&base);
        bool ok = IntNextPrime(&next, &value);
    
        bool result = ok && (IntToU64(&next) == 2);
    
        IntDeinit(&value);
        bool ok = IntNextPrime(&next, &value);
    
        bool result = ok && (IntToU64(&next) == 11);
    
        IntDeinit(&value);
        bool ok = IntNextPrime(&next, &value);
    
        bool result = ok && (IntToU64(&next) == 11);
    
        IntDeinit(&value);
    
        bool ok = IntModSub(&result_value, &a, &b, &m);
        ok      = ok && (IntToU64(&result_value) == 4);
    
        IntDeinit(&a);
        bool ok = IntPowMod(&result_value, &base, 13u, &mod);
    
        bool result = ok && (IntToU64(&result_value) == 12);
    
        IntDeinit(&base);
        bool ok = IntPowMod(&result_value, &base, 0u, &mod);
    
        bool result = ok && (IntToU64(&result_value) == 1);
    
        IntDeinit(&base);
        bool ok = IntPowMod(&result_value, &base, 64u, &mod);
    
        bool result = ok && (IntToU64(&result_value) == 582344008);
    
        IntDeinit(&base);
    
        bool ok = IntMul(&r, &x, &y);
        ok      = ok && IntToU64(&r) == 123456ull * 7891011ull;
    
        IntDeinit(&x);
    
        bool ok = IntAdd(&r, &x, &y);
        ok      = ok && IntToU64(&r) == 0xFFFFFFFFull + 0xFFFFFFFFull;
    
        IntDeinit(&x);
    
        bool ok = IntSub(&r, &x, &y);
        ok      = ok && IntToU64(&r) == 17u;
    
        IntDeinit(&x);
    
        bool ok = IntAdd(&r, &x, 250u);
        ok      = ok && IntToU64(&r) == 750u;
    
        IntDeinit(&x);
    
        bool ok = IntMul(&r, &x, 13u);
        ok      = ok && IntToU64(&r) == 6500u;
    
        IntDeinit(&x);
    
        bool ok = IntSub(&r, &x, 250u); // int_sub_u64 frees rhs on success (1187)
        ok      = ok && IntToU64(&r) == 750u;
    
        IntDeinit(&x);
    
        bool ok = IntDivMod(&q, &r, &dvd, &dvs);
        ok      = ok && IntToU64(&q) == 1000003u / 101u && IntToU64(&r) == 1000003u % 101u;
    
        IntDeinit(&dvd);
    
        bool ok = IntDivMod(&q, &r, &dvd, &dvs);
        ok      = ok && IntIsZero(&q) && IntToU64(&r) == 50u;
    
        IntDeinit(&dvd);
    
        bool ok = IntDiv(&r, &dvd, &dvs); // int_div frees remainder on success (1445)
        ok      = ok && IntToU64(&r) == 1000003u / 101u;
    
        IntDeinit(&dvd);
    
        bool ok = IntDivExact(&r, &dvd, &dvs); // frees remainder on success (1474)
        ok      = ok && IntToU64(&r) == 125u;
    
        IntDeinit(&dvd);
        ok      = ok && IntDivExact(&r2, &dvd, 8u);
        ok      = ok && IntDiv(&r3, &dvd, (i64)8);
        ok      = ok && IntToU64(&r1) == 125u && IntToU64(&r2) == 125u && IntToU64(&r3) == 125u;
    
        IntDeinit(&dvd);
        ok      = ok && IntMod(&m1, &dvd, 100u);
        ok      = ok && IntMod(&m2, &dvd, (i64)100);
        ok      = ok && IntToU64(&q) == 10u && IntToU64(&r) == 3u;
        ok      = ok && IntToU64(&m1) == 3u && IntToU64(&m2) == 3u;
        ok      = ok && IntMod(&m2, &dvd, (i64)100);
        ok      = ok && IntToU64(&q) == 10u && IntToU64(&r) == 3u;
        ok      = ok && IntToU64(&m1) == 3u && IntToU64(&m2) == 3u;
    
        IntDeinit(&dvd);
    
        bool ok = IntMod(&r, &dvd, &dvs); // int_mod frees quotient on success (1598)
        ok      = ok && IntToU64(&r) == 3u;
    
        IntDeinit(&dvd);
    
        bool ok = IntPow(&r, &base, 13u);
        ok      = ok && IntToU64(&r) == 1594323u; // 3^13
    
        IntDeinit(&base);
    
        bool ok = IntGCD(&r, &x, &y);
        ok      = ok && IntToU64(&r) == 6u;
    
        IntDeinit(&x);
    
        bool ok = IntLCM(&r, &x, &y);
        ok      = ok && IntToU64(&r) == 42u;
    
        IntDeinit(&x);
    
        bool ok = IntRootRem(&root, &rem, &v, 3); // 100^3 = 1e6
        ok      = ok && IntToU64(&root) == 100u && IntIsZero(&rem);
    
        IntDeinit(&v);
    
        bool ok = IntRootRem(&root, &rem, &v, 3); // cbrt(1000)=10 exact actually
        ok      = ok && IntToU64(&root) == 10u && IntIsZero(&rem);
    
        // and a genuinely inexact one
        Int v2 = IntFrom(999u, a);
        ok     = ok && IntRootRem(&root, &rem, &v2, 3);
        ok     = ok && IntToU64(&root) == 9u && IntToU64(&rem) == 999u - 729u;
    
        IntDeinit(&v);
    
        bool ok = IntRoot(&r, &v, 3); // frees remainder on success (1877)
        ok      = ok && IntToU64(&r) == 100u;
    
        IntDeinit(&v);
    
        bool ok = IntModAdd(&r, &x, &y, &m); // frees ar/br/sum on success (2042-2044)
        ok      = ok && IntToU64(&r) == (12345u % 1009u + 67890u % 1009u) % 1009u;
    
        IntDeinit(&x);
    
        bool ok = IntModSub(&r, &x, &y, &m);
        ok      = ok && IntToU64(&r) == 800u;
    
        IntDeinit(&x);
    
        bool ok = IntModSub(&r, &x, &y, &m);
        ok      = ok && IntToU64(&r) == 1009u - 800u;
    
        IntDeinit(&x);
    
        bool ok = IntModMul(&r, &x, &y, &m); // frees ar/br/prod (2126-2128)
        ok      = ok && IntToU64(&r) == (12345ull * 67890ull) % 1009ull;
    
        IntDeinit(&x);
    
        bool ok = IntSquareMod(&r, &x, &m);
        ok      = ok && IntToU64(&r) == (12345ull * 12345ull) % 1009ull;
    
        IntDeinit(&x);
    
        bool ok = IntPowMod(&r, &base, 13u, &m);
        ok      = ok && IntToU64(&r) == 96889010407ull % 1000000007ull; // 7^13 mod m
    
        IntDeinit(&base);
    
        bool ok = IntPowMod(&r, &base, &exp, &m);
        ok      = ok && IntToU64(&r) == 96889010407ull % 1000000007ull;
    
        IntDeinit(&base);
    
        bool ok = IntModInv(&r, &v, &m);
        ok      = ok && (IntToU64(&r) * 17u) % 3120u == 1u;
    
        IntDeinit(&v);
    
        bool ok = IntModInv(&r, &v, &m);
        ok      = ok && IntToU64(&r) == 5u;
    
        IntDeinit(&v);
    
        bool ok = IntModSqrt(&r, &v, &m);
        u64  rv = IntToU64(&r);
        ok      = ok && (rv * rv) % 7u == 2u;
    
        bool ok = IntModSqrt(&r, &v, &m);
        u64  rv = IntToU64(&r);
        ok      = ok && (rv * rv) % 17u == 2u;
    
        bool ok = IntModSqrt(&r, &v, &m);
        u64  rv = IntToU64(&r);
        ok      = ok && (rv * rv) % 41u == 10u;
        Int r = IntFrom(9u, a);
    
        bool ok = IntModSqrt(&r, &v, &m) && IntToU64(&r) == 1u;
    
        IntDeinit(&v);
    
        bool ok = IntNextPrime(&r, &v);
        ok      = ok && IntToU64(&r) == 1000003u;
    
        IntDeinit(&v);
        Int r1 = IntFrom(7u, a);
    
        bool ok = IntNextPrime(&r0, &v0) && IntToU64(&r0) == 2u;
        ok      = ok && IntNextPrime(&r1, &v1) && IntToU64(&r1) == 3u;
    
        bool ok = IntNextPrime(&r0, &v0) && IntToU64(&r0) == 2u;
        ok      = ok && IntNextPrime(&r1, &v1) && IntToU64(&r1) == 3u;
    
        IntDeinit(&v0);
    
        bool ok = IntMul(&r, &x, &y);
        ok      = ok && IntToU64(&r) == 123456ull * 7891011ull;
    
        IntDeinit(&x);
    
        bool ok = IntDivExact(&r, &dvd, (i64)8);
        ok      = ok && IntToU64(&r) == 125u;
    
        IntDeinit(&dvd);
                bool found = IntModSqrt(&r, &v, &m);
                if (found) {
                    u64 rv = IntToU64(&r);
                    ok     = ok && (rv * rv) % p == vv;
                }
    
        bool result = IntBitLength(&value) == 4;
        result      = result && (IntToU64(&value) == 13);
        result      = result && (ZstrCompare(StrBegin(&text), "1101") == 0);
        Str text  = IntToBinary(&value);
    
        bool result = IntToU64(&value) == 11;
        result      = result && (ZstrCompare(StrBegin(&text), "1011") == 0);
        Str text  = IntToStrRadix(&value, 36, false);
    
        bool result = IntToU64(&value) == 1295;
        result      = result && (ZstrCompare(StrBegin(&text), "zz") == 0);
        bool result = IntBitLength(&zero) == 0;
        result      = result && IntIsZero(&zero);
        result      = result && (IntToU64(&zero, &error) == 0);
        result      = result && !error;
        result      = result && (ZstrCompare(StrBegin(&text), "0") == 0);
        Int value = IntFromBinary("0b1010_0011", ALLOCATOR_OF(&alloc));
    
        bool result = IntToU64(&value) == 163;
        result      = result && (IntBitLength(&value) == 8);
        Str text  = IntToOctStr(&value);
    
        bool result = IntToU64(&value) == 493;
        result      = result && (ZstrCompare(StrBegin(&text), "755") == 0);
    
    bool test_int_to_u64_overflow(void) {
        WriteFmt("Testing IntToU64 overflow handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        bool result = !IntTryToU64(&value, &out);
        result      = result && (IntToU64(&value, &error) == 0);
        result      = result && error;
        result      = result && (out == 0);
        bool ok = IntMul(&product, &a, &b);
    
        bool result = ok && (IntToU64(&product) == (u64)273u);
        /* 273 = 0b100010001, 9 significant bits */
        result = result && (IntBitLength(&product) == 9);
        Int  value  = IntInit(ALLOCATOR_OF(&alloc));
        bool ok     = IntTryFromStrRadix(&value, "7", 10);
        bool result = ok && (IntToU64(&value) == 7);
    
        IntDeinit(&value);
        bool rejected_radix = !IntTryFromStrRadix(&invalid, "5", 37);
    
        bool result = ok_valid && (IntToU64(&valid) == 5);
        result      = result && rejected_radix;
        result      = result && IntIsZero(&invalid);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_binary_str(&out, &text);
        bool result = ok && (IntToU64(&out) == 5u);
    
        StrDeinit(&text);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_binary_str(&out, &text);
        bool result = ok && (IntToU64(&out) == 5u);
    
        StrDeinit(&text);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_binary_str(&out, &text);
        bool result = ok && (IntToU64(&out) == 13u);
    
        StrDeinit(&text);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_oct_str_str(&out, &text);
        bool result = ok && (IntToU64(&out) == 15u);
    
        StrDeinit(&text);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_oct_str_str(&out, &text);
        bool result = ok && (IntToU64(&out) == 15u);
    
        StrDeinit(&text);
        Int  out    = IntInit(&alloc.base);
        bool ok     = int_try_from_oct_str_str(&out, &text);
        bool result = ok && (IntToU64(&out) == 15u);
    
        StrDeinit(&text);
        bool parsed = IntTryFromStrRadix(&out, "AZ", 36);
    
        bool result = parsed && (IntToU64(&out) == 395);
    
        IntDeinit(&out);
        bool parsed = IntTryFromStrRadix(&out, "FF", 16);
    
        bool result = parsed && (IntToU64(&out) == 255);
    
        IntDeinit(&out);
        bool parsed = IntTryFromStr(&out, &text);
    
        bool result = parsed && (IntToU64(&out) == 123);
    
        IntDeinit(&out);
        bool parsed = IntTryFromStr(&out, &text);
    
        bool result = parsed && (IntToU64(&out) == 5);
    
        IntDeinit(&out);
    
        bool result = ok;
        result      = result && (IntToU64(&value) == 123);
    
        IntDeinit(&value);
    
        bool result = ok;
        result      = result && (IntToU64(&value) == 5);
    
        IntDeinit(&value);
        bool ok     = IntTryFromStr(&out, text);
        bool result = ok;
        result      = result && (IntToU64(&out) == 5u);
    
        IntDeinit(&out);
        bool ok     = IntTryFromStr(&out, text);
        bool result = ok;
        result      = result && (IntToU64(&out) == 42u);
    
        IntDeinit(&out);
        bool ok  = IntTryFromStrRadix(&out, "+5", (u8)10);
    
        bool result = ok && (IntToU64(&out) == 5);
    
        IntDeinit(&out);
        bool ok  = IntTryFromOctStr(&out, "17");
    
        bool result = ok && (IntToU64(&out) == 15);
    
        IntDeinit(&out);
        bool ok  = IntTryFromOctStr(&out, "017");
    
        bool result = ok && (IntToU64(&out) == 15);
    
        IntDeinit(&out);
        Int value   = IntFromBytesLE(bytes, sizeof(bytes), ALLOCATOR_OF(&alloc));
    
        bool result = (IntToU64(&value) == 5);
        result      = result && (IntBitLength(&value) == 3);
        Int value  = IntFromStr(&digits, &alloc.base);
    
        bool result = (IntToU64(&value) == (u64)12345u);
    
        IntDeinit(&value);
        Int value  = IntFromStrRadix(&digits, (u8)16u, &alloc.base);
    
        bool result = (IntToU64(&value) == (u64)255u);
    
        IntDeinit(&value);
    
        bool result = ok;
        result      = result && (IntToU64(&value) == 255u);
    
        StrDeinit(&hex);
        Int v       = IntFromBytesBE(bytes, sizeof(bytes), &dbg);
    
        bool ok = IntToU64(&v) == 0x01020304u;
    
        IntDeinit(&v);
    
        bool ok = IntTryFromStr(&out, "123456789");
        ok      = ok && IntToU64(&out) == 123456789u;
    
        IntDeinit(&out);
        bool ok     = IntRootRem(&root, &remainder, &value, 1);
        bool result = ok;
        result      = result && (IntToU64(&root) == 12345);
        result      = result && (IntCompare(&remainder, 0) == 0);
    
        bool ok     = IntShiftRight(&value, 0);
        bool result = ok && (IntToU64(&value) == 5);
    
        IntDeinit(&value);
        bool ok = IntPowMod(&result_value, &base, 20u, &mod);
    
        bool result = ok && (IntToU64(&result_value) == 3);
    
        IntDeinit(&base);
Last updated on