Skip to content

IntLog2

Description

Compute floor(log2(value)).

This public macro supports both forms:

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

Parameters

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

Usage example (from documentation)

  u64 hi = IntLog2(&value);

Success

Returns the zero-based index of the highest set bit as a u64. The integer is not modified.

Failure

Returns 0 when value is zero. With the two-argument form *error is set to true; with the one-argument form the caller cannot distinguish zero-input from log2(1).

Usage example (Cross-references)

Usage examples (Cross-references)
    
    bool test_int_log2(void) {
        WriteFmt("Testing IntLog2\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        bool error = true;
    
        bool result = IntLog2(&value, &error) == 10;
        result      = result && !error;
    
    bool test_int_log2_zero(void) {
        WriteFmt("Testing IntLog2 zero handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        bool error = false;
    
        bool result = IntLog2(&value, &error) == 0;
        result      = result && error;
Last updated on