Skip to content

IntBitLength

Description

Number of significant bits required to represent the magnitude of an integer.

Parameters

Name Direction Description
value in Integer to inspect.

Success

Returns the bit length as a u64. Returns 0 when value is zero. The integer is not modified.

Failure

Function cannot fail. An invalid value pointer is a caller bug and aborts via LOG_FATAL.

Usage example (Cross-references)

Usage examples (Cross-references)
    static bool int_is_one(const Int *value) {
        ValidateInt(value);
        return IntBitLength(value) == 1 && BitVecGet(INT_BITS(value), 0);
    }
    }
    
    u64 IntBitLength(const Int *value) {
        return int_significant_bits(value);
    }
    
    u64 IntByteLength(const Int *value) {
        u64 bits = IntBitLength(value);
        return bits == 0 ? 0 : CEIL_DIV(bits, 8u);
    }
        }
    
        *out = IntBitLength(value) - 1;
        return true;
    }
    
    bool IntIsZero(const Int *value) {
        return IntBitLength(value) == 0;
    }
    bool IntFitsU64(const Int *value) {
        ValidateInt(value);
        return IntBitLength(value) <= 64;
    }
        ValidateInt(value);
    
        return !IntIsZero(value) && IntBitLength(value) == IntTrailingZeroCount(value) + 1;
    }
        ValidateInt(value);
    
        u64       bits      = IntBitLength(value);
        u64       bytes     = bits == 0 ? 0 : CEIL_DIV(bits, 8u);
        const u8 *magnitude = (const u8 *)BitVecData(INT_BITS(value));
        ValidateInt(b);
    
        u64 a_bits = IntBitLength(a);
        u64 b_bits = IntBitLength(b);
    
        u64 a_bits = IntBitLength(a);
        u64 b_bits = IntBitLength(b);
    
        if (a_bits < b_bits) {
        ValidateInt(lhs);
    
        if (IntBitLength(lhs) > 64) {
            return 1;
        }
        ValidateInt(value);
    
        u64 bits = IntBitLength(value);
    
        if (positions == 0) {
        ValidateInt(value);
    
        u64 bits = IntBitLength(value);
    
        if (positions == 0) {
        ValidateInt(b);
    
        u64  a_bits   = IntBitLength(a);
        u64  b_bits   = IntBitLength(b);
        u64  max_bits = MAX2(a_bits, b_bits);
    
        u64  a_bits   = IntBitLength(a);
        u64  b_bits   = IntBitLength(b);
        u64  max_bits = MAX2(a_bits, b_bits);
        Int  temp;
        }
    
        u64  a_bits = IntBitLength(a);
        u64  b_bits = IntBitLength(b);
        Int  temp;
    
        u64  a_bits = IntBitLength(a);
        u64  b_bits = IntBitLength(b);
        Int  temp;
        bool borrow = false;
        ValidateInt(b);
    
        u64 b_bits = IntBitLength(b);
        Int acc    = IntInit(IntAllocator(result));
    
        if (int_compare(&normalized_dividend, &normalized_divisor) >= 0) {
            u64 dividend_bits = IntBitLength(&normalized_dividend);
            u64 divisor_bits  = IntBitLength(&normalized_divisor);
        if (int_compare(&normalized_dividend, &normalized_divisor) >= 0) {
            u64 dividend_bits = IntBitLength(&normalized_dividend);
            u64 divisor_bits  = IntBitLength(&normalized_divisor);
    
            IntDeinit(&q);
                    r = next;
    
                    if (IntBitLength(&q) < bit + 1 && !BitVecResize(INT_BITS(&q), bit + 1)) {
                        IntDeinit(&shifted);
                        goto cleanup;
        }
    
        u64 bits       = IntBitLength(value);
        u64 high_shift = bits / degree;
        Int low        = IntInit(IntAllocator(root));
        ValidateInt(value);
    
        if (IntIsZero(value) || IntBitLength(value) == 1) {
            return true;
        }
    
    bool test_int_bit_length(void) {
        WriteFmt("Testing IntBitLength\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int value = IntFromBinary("00101000", &alloc.base);
    
        bool result = IntBitLength(&value) == 6;
    
        IntDeinit(&value);
        Str text  = IntToBinary(&value);
    
        bool result = IntBitLength(&value) == 4;
        result      = result && (IntToU64(&value) == 13);
        result      = result && (ZstrCompare(StrBegin(&text), "1101") == 0);
        bool error = true;
    
        bool result = IntBitLength(&zero) == 0;
        result      = result && IntIsZero(&zero);
        result      = result && (IntToU64(&zero, &error) == 0);
    
        bool result = IntToU64(&value) == 163;
        result      = result && (IntBitLength(&value) == 8);
    
        IntDeinit(&value);
    
        bool result = IntIsZero(&value);
        result      = result && (IntBitLength(&value) == 0);
    
        IntDeinit(&value);
    
        bool result = IntIsZero(&value);
        result      = result && (IntBitLength(&value) == 0);
    
        IntDeinit(&value);
    
        bool result = IntToU64(&value) == 48;
        result      = result && (IntBitLength(&value) == 6);
    
        IntDeinit(&value);
    
        bool result = IntToU64(&value) == 3;
        result      = result && (IntBitLength(&value) == 2);
    
        IntDeinit(&value);
Last updated on