Skip to content

IntShiftLeft

Description

Shift an integer left by the given number of bit positions.

Parameters

Name Direction Description
value in Integer to modify
positions in Number of zero bits to append on the right

Usage example (from documentation)

  IntShiftLeft(&value, 8);

Success

Returns true. *value has been shifted in place.

Failure

Returns false on allocator OOM while growing the significand. *value is left untouched.

Usage example (Cross-references)

Usage examples (Cross-references)
    
        if (binexp > 0) {
            if (!IntShiftLeft(&out->significand, (u64)binexp)) {
                return false;
            }
    
        for (u64 i = 0; i < len; i++) {
            if (!IntShiftLeft(&result, 8) || !int_add_u64_in_place(&result, bytes[i])) {
                IntDeinit(&result);
                return IntInit(alloc);
    }
    
    bool IntShiftLeft(Int *value, u64 positions) {
        ValidateInt(value);
            }
            int_normalize(&partial);
            if (!IntShiftLeft(&partial, i) || !int_add(&next, &acc, &partial)) {
                IntDeinit(&acc);
                IntDeinit(&partial);
                Int shifted = IntInit(IntAllocator(quotient));
    
                if (!int_try_clone_value(&shifted, &normalized_divisor) || !IntShiftLeft(&shifted, bit)) {
                    IntDeinit(&shifted);
                    goto cleanup;
        }
    
        if (!IntShiftLeft(&high, high_shift)) {
            IntDeinit(&low);
            IntDeinit(&high);
        Int big   = IntFrom(1, &alloc.base);
    
        IntShiftLeft(&big, 64);
    
        bool result = IntFitsU64(&small);
        Int zero  = IntInit(&alloc.base);
    
        IntShiftLeft(&power, 20);
    
        bool result = IntIsPowerOfTwo(&one);
        bool error = false;
    
        IntShiftLeft(&value, 64);
    
        bool result = !IntTryToU64(&value, &out);
        result      = result && (IntToU64(&clone) == 11);
    
        IntShiftLeft(&original, 1);
    
        result = result && !IntEQ(&clone, &original);
        Int big   = IntFrom(1, &alloc.base);
    
        IntShiftLeft(&big, 80);
    
        bool result = (IntCompare(&value, &same) == 0);
        Int small = IntFrom(42u, &alloc.base);
        Int large = IntFrom(1u, &alloc.base);
        IntShiftLeft(&large, 80);
        Int decimal = IntFromStr("12345678901234567890", &alloc.base);
    
    bool test_int_shift_left_grows(void) {
        WriteFmt("Testing IntShiftLeft\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFrom(3, &alloc.base);
    
        IntShiftLeft(&value, 4);
    
        bool result = IntToU64(&value) == 48;
    
    bool test_int_shift_left_null(void) {
        WriteFmt("Testing IntShiftLeft NULL handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        IntShiftLeft(NULL, 1);
        DefaultAllocatorDeinit(&alloc);
        return false;
Last updated on