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;
    }
        Int remainder     = IntInit(alloc);
    
        if (!int_try_from_u64(&chunk_divisor, chunk, alloc) || !IntReserve(&quotient, IntBitLength(value))) {
            IntDeinit(&chunk_divisor);
            IntDeinit(&quotient);
        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);
        }
    
        u64 a_bits = IntBitLength(a);
        u64 b_bits = IntBitLength(b);
    
        u64 a_bits = IntBitLength(a);
        u64 b_bits = IntBitLength(b);
    
        if (!BitVecResize(INT_BITS(result), a_bits)) {
    
        {
            u64 a_bits  = IntBitLength(a);
            u64 b_bits  = IntBitLength(b);
            u64 a_words = (a_bits + 63u) / 64u;
        {
            u64 a_bits  = IntBitLength(a);
            u64 b_bits  = IntBitLength(b);
            u64 a_words = (a_bits + 63u) / 64u;
            u64 b_words = (b_bits + 63u) / 64u;
    
        {
            u64 dividend_bits = IntBitLength(dividend);
            u64 divisor_bits  = IntBitLength(divisor);
            u64 r_words       = (divisor_bits + 1u + 63u) / 64u;
        {
            u64 dividend_bits = IntBitLength(dividend);
            u64 divisor_bits  = IntBitLength(divisor);
            u64 r_words       = (divisor_bits + 1u + 63u) / 64u;
        }
    
        u64 bits       = IntBitLength(value);
        u64 high_shift = bits / degree;
        Int low        = IntInit(IntAllocator(root));
        ValidateInt(value);
    
        if (IntIsZero(value) || IntBitLength(value) == 1) {
            return true;
        }
        Int lhs = IntFrom(0x8000000000000000u, &alloc.base);
    
        bool fail = (IntBitLength(&lhs) != 64); // sanity: exactly 64 bits.
        fail      = fail || (IntCompare(&lhs, 0x8000000000000000u) != 0);
    
        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(&sum) == 256);
        result      = result && (IntBitLength(&sum) == 9);
    
        IntDeinit(&a);
        Int v255 = IntFrom(255, &alloc.base);
    
        bool result = IntBitLength(&v255) == 8;
        result      = result && (IntToU64(&v255) == 255);
    
        bool result = IntToU64(&value) == 48;
        result      = result && (IntBitLength(&value) == 6);
    
        IntDeinit(&value);
    
        bool result = IntToU64(&value) == 3;
        result      = result && (IntBitLength(&value) == 2);
    
        IntDeinit(&value);
        bool result = ok && IntIsZero(&product);
        result      = result && (IntToU64(&product) == 0u);
        result      = result && (IntBitLength(&product) == 0u);
    
        IntDeinit(&a);
    
        bool fail = (IntCompare(&value, 5u) != 0);
        fail      = fail || (IntBitLength(&value) != 3);
    
        IntDeinit(&value);
    
        bool fail = (IntCompare(&value, 1u) != 0);
        fail      = fail || (IntBitLength(&value) != 1);
    
        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 = ok && (IntToU64(&product) == (u64)273u);
        /* 273 = 0b100010001, 9 significant bits */
        result = result && (IntBitLength(&product) == 9);
    
        IntDeinit(&a);
    
        bool result = (IntToU64(&value) == 5);
        result      = result && (IntBitLength(&value) == 3);
    
        IntDeinit(&value);
    
    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);
        Int value = IntFrom((u64)40u, &alloc.base);
    
        bool result = (IntBitLength(&value) == 6u);
        result      = result && (IntTrailingZeroCount(&value) == 3u);
    ///
    static bool test_m28_bit_length_invalid_deadend(void) {
        WriteFmt("Testing IntBitLength validation on invalid Int\n");
    
        Int invalid = {0};
        Int invalid = {0};
    
        (void)IntBitLength(&invalid);
    
        return false;
Last updated on