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 int_pow_u64(result, base, IntToU64(exponent));
    }
            return 0;
        }
        rem = IntToU64(&remainder);
    
        IntDeinit(&divisor_value);
    
        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 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);
        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);
Last updated on