package core:math/bits

⌘K
Ctrl+K
or
/

    Overview

    Bit-level operations, including the ability to set or toggle individual bits in an integer.

    Index

    Types (0)

    This section is empty.

    Variables (0)

    This section is empty.

    Procedures (88)

    Types

    This section is empty.

    Constants

    I16_MAX ¶

    I16_MAX: int : 1 << 15 - 1
     

    The maximum value held by an i16. The same value as max(i16), except untyped.

    I16_MIN ¶

    I16_MIN: int : -1 << 15
     

    The minimum value held by an i16. The same value as min(i16), except untyped.

    I32_MAX ¶

    I32_MAX: int : 1 << 31 - 1
     

    The maximum value held by an i32. The same value as max(i32), except untyped.

    I32_MIN ¶

    I32_MIN: int : -1 << 31
     

    The minimum value held by an i32. The same value as min(i32), except untyped.

    I64_MAX ¶

    I64_MAX: int : 1 << 63 - 1
     

    The maximum value held by an i64. The same value as max(i64), except untyped.

    I64_MIN ¶

    I64_MIN: int : -1 << 63
     

    The minimum value held by an i64. The same value as min(i64), except untyped.

    I8_MAX ¶

    I8_MAX: int : 1 << 7 - 1
     

    The maximum value held by an i8. The same value as max(i8), except untyped.

    I8_MIN ¶

    I8_MIN: int : -1 << 7
     

    The minimum value held by an i8. The same value as min(i8), except untyped.

    INT_MAX ¶

    INT_MAX: int : I64_MAX when size_of(int) == 8 else I32_MAX
     

    The maximum value held by an int. The same value as max(int), except untyped.

    INT_MIN ¶

    INT_MIN: int : I64_MIN when size_of(int) == 8 else I32_MIN
     

    The minimum value held by an int. The same value as min(int), except untyped.

    U16_MAX ¶

    U16_MAX: int : 1 << 16 - 1
     

    The maximum value held by a u16. The same value as max(u16), except untyped.

    U16_MIN ¶

    U16_MIN: int : 0
     

    The minimum value held by a u16. The same value as min(u16), except untyped.

    U32_MAX ¶

    U32_MAX: int : 1 << 32 - 1
     

    The maximum value held by a u32. The same value as max(u32), except untyped.

    U32_MIN ¶

    U32_MIN: int : 0
     

    The minimum value held by a u32. The same value as min(u32), except untyped.

    U64_MAX ¶

    U64_MAX: int : 1 << 64 - 1
     

    The maximum value held by a u64. The same value as max(u64), except untyped.

    U64_MIN ¶

    U64_MIN: int : 0
     

    The minimum value held by a u64. The same value as min(u64), except untyped.

    U8_MAX ¶

    U8_MAX: int : 1 << 8 - 1
     

    The maximum value held by a u8. The same value as max(u8), except untyped.

    U8_MIN ¶

    U8_MIN: int : 0
     

    The minimum value held by a u8. The same value as min(u8), except untyped.

    UINT_MAX ¶

    UINT_MAX: int : U64_MAX when size_of(uint) == 8 else U32_MAX
     

    The maximum value held by a uint. The same value as max(uint), except untyped.

    UINT_MIN ¶

    UINT_MIN: int : 0
     

    The minimum value held by a uint. The same value as min(uint), except untyped.

    Variables

    This section is empty.

    Procedures

    add_u32 ¶

    add_u32 :: proc "contextless" (x, y, carry: u32) -> (sum, carry_out: u32) {…}
     

    Add with carry

    Inputs:
    x: The unsigned integer y: Another unsigned integer carry: Carry in

    Returns:
    sum: The sum carry_out: Carry out

    Related Procedure Groups

    add_u64 ¶

    add_u64 :: proc "contextless" (x, y, carry: u64) -> (sum, carry_out: u64) {…}
     

    Add with carry

    Inputs:
    x: The unsigned integer y: Another unsigned integer carry: Carry in

    Returns:
    sum: The sum carry_out: Carry out

    Related Procedure Groups

    add_uint ¶

    add_uint :: proc "contextless" (x, y, carry: uint) -> (sum, carry_out: uint) {…}
     

    Add with carry

    Inputs:
    x: The unsigned integer y: Another unsigned integer carry: Carry in

    Returns:
    sum: The sum carry_out: Carry out

    Related Procedure Groups

    bitfield_extract_i128 ¶

    bitfield_extract_i128 :: proc "contextless" (value: i128, offset, bits: uint) -> i128 {…}
     

    Extracts bits from a signed integer

    Inputs:
    value: Signed integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_i16 ¶

    bitfield_extract_i16 :: proc "contextless" (value: i16, offset, bits: uint) -> i16 {…}
     

    Extracts bits from a signed integer

    Inputs:
    value: Signed integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_i32 ¶

    bitfield_extract_i32 :: proc "contextless" (value: i32, offset, bits: uint) -> i32 {…}
     

    Extracts bits from a signed integer

    Inputs:
    value: Signed integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_i64 ¶

    bitfield_extract_i64 :: proc "contextless" (value: i64, offset, bits: uint) -> i64 {…}
     

    Extracts bits from a signed integer

    Inputs:
    value: Signed integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_i8 ¶

    bitfield_extract_i8 :: proc "contextless" (value: i8, offset, bits: uint) -> i8 {…}
     

    Extracts bits from a signed integer

    Inputs:
    value: Signed integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_int ¶

    bitfield_extract_int :: proc "contextless" (value: int, offset, bits: uint) -> int {…}
     

    Extracts bits from a signed integer

    Inputs:
    value: Signed integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_u128 ¶

    bitfield_extract_u128 :: proc "contextless" (value: u128, offset, bits: uint) -> (res: u128) {…}
     

    Extracts bits from an unsigned integer

    Inputs:
    value: Unsigned integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_u16 ¶

    bitfield_extract_u16 :: proc "contextless" (value: u16, offset, bits: uint) -> (res: u16) {…}
     

    Extracts bits from an unsigned integer

    Inputs:
    value: Unsigned integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_u32 ¶

    bitfield_extract_u32 :: proc "contextless" (value: u32, offset, bits: uint) -> (res: u32) {…}
     

    Extracts bits from an unsigned integer

    Inputs:
    value: Unsigned integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_u64 ¶

    bitfield_extract_u64 :: proc "contextless" (value: u64, offset, bits: uint) -> (res: u64) {…}
     

    Extracts bits from an unsigned integer

    Inputs:
    value: Unsigned integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_u8 ¶

    bitfield_extract_u8 :: proc "contextless" (value: u8, offset, bits: uint) -> (res: u8) {…}
     

    Extracts bits from an unsigned integer

    Inputs:
    value: Unsigned integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_extract_uint ¶

    bitfield_extract_uint :: proc "contextless" (value: uint, offset, bits: uint) -> (res: uint) {…}
     

    Extracts bits from an unsigned integer

    Inputs:
    value: Unsigned integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    Related Procedure Groups

    bitfield_insert_i128 ¶

    bitfield_insert_i128 :: proc "contextless" (base, insert: i128, offset, bits: uint) -> (res: i128) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_i16 ¶

    bitfield_insert_i16 :: proc "contextless" (base, insert: i16, offset, bits: uint) -> (res: i16) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_i32 ¶

    bitfield_insert_i32 :: proc "contextless" (base, insert: i32, offset, bits: uint) -> (res: i32) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_i64 ¶

    bitfield_insert_i64 :: proc "contextless" (base, insert: i64, offset, bits: uint) -> (res: i64) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_i8 ¶

    bitfield_insert_i8 :: proc "contextless" (base, insert: i8, offset, bits: uint) -> (res: i8) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_int ¶

    bitfield_insert_int :: proc "contextless" (base, insert: int, offset, bits: uint) -> (res: int) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_u128 ¶

    bitfield_insert_u128 :: proc "contextless" (base, insert: u128, offset, bits: uint) -> (res: u128) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_u16 ¶

    bitfield_insert_u16 :: proc "contextless" (base, insert: u16, offset, bits: uint) -> (res: u16) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_u32 ¶

    bitfield_insert_u32 :: proc "contextless" (base, insert: u32, offset, bits: uint) -> (res: u32) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_u64 ¶

    bitfield_insert_u64 :: proc "contextless" (base, insert: u64, offset, bits: uint) -> (res: u64) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_u8 ¶

    bitfield_insert_u8 :: proc "contextless" (base, insert: u8, offset, bits: uint) -> (res: u8) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    bitfield_insert_uint ¶

    bitfield_insert_uint :: proc "contextless" (base, insert: uint, offset, bits: uint) -> (res: uint) {…}
     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    Related Procedure Groups

    byte_swap ¶

    byte_swap :: intrinsics.byte_swap
    byte_swap :: proc(x: $T) -> T where type_is_integer(T) || type_is_float(T) {…}
     

    Reverses the bytes from ascending order to descending order e.g. 0xfe_ed_01_12 -> 0x12_01_ed_fe

    count_leading_zeros ¶

    count_leading_zeros :: intrinsics.count_leading_zeros
    count_leading_zeros :: proc(x: $T) -> T where type_is_integer(T) || type_is_simd_vector(T) {…}
     

    Counts the number of leading unset bits (0s) until a set bit (1) is seen or all bits have been counted.

    count_ones ¶

    count_ones :: intrinsics.count_ones
    count_ones :: proc(x: $T) -> T where type_is_integer(T) || type_is_simd_vector(T) {…}
     

    Counts the number of set bits (1s).

    count_trailing_zeros ¶

    count_trailing_zeros :: intrinsics.count_trailing_zeros
    count_trailing_zeros :: proc(x: $T) -> T where type_is_integer(T) || type_is_simd_vector(T) {…}
     

    Counts the number of trailing unset bits (0s) until a set bit (1) is seen or all bits have been counted.

    count_zeros ¶

    count_zeros :: intrinsics.count_zeros
    count_zeros :: proc(x: $T) -> T where type_is_integer(T) || type_is_simd_vector(T) {…}
     

    Counts the number of unset bits (0s).

    div_u32 ¶

    div_u32 :: proc(hi, lo, y: u32) -> (quo, rem: u32) {…}
     

    Divide a 64-bit unsigned integer (in two 32-bit words) by a 32-bit divisor

    Inputs:
    hi: High word of 64-bit integer lo: Low word of 64-bit integer y: Divisor

    Returns:
    quo: 32-bit quotient rem: 32-bit remainder

    Related Procedure Groups

    div_u64 ¶

    div_u64 :: proc(hi, lo, y: u64) -> (quo, rem: u64) {…}
     

    Divide a 128-bit unsigned integer (in two 64-bit words) by a 64-bit divisor

    Inputs:
    hi: High word of 128-bit integer lo: Low word of 128-bit integer y: Divisor

    Returns:
    quo: 64-bit quotient rem: 64-bit Remainder

    Related Procedure Groups

    div_uint ¶

    div_uint :: proc(hi, lo, y: uint) -> (quo, rem: uint) {…}
     

    Divide an unsigned integer (in two words) by a divisor

    Inputs:
    hi: High word of input lo: Low word of input y: Divisor

    Returns:
    quo: Quotient rem: Remainder

    Related Procedure Groups

    from_be_u16 ¶

    from_be_u16 :: proc "contextless" (i: u16) -> u16 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a little endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    from_be_u32 ¶

    from_be_u32 :: proc "contextless" (i: u32) -> u32 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a little endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    from_be_u64 ¶

    from_be_u64 :: proc "contextless" (i: u64) -> u64 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a little endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    from_be_u8 ¶

    from_be_u8 :: proc "contextless" (i: u8) -> u8 {…}
     

    Returns unsigned integer i

    NOTE: A byte has no endianness, so from_be_u8 exists to be complementary to from_be_*.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i

    from_be_uint ¶

    from_be_uint :: proc "contextless" (i: uint) -> uint {…}
     

    Returns unsigned integer i, byte-swapped if we're on a little endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    from_le_u16 ¶

    from_le_u16 :: proc "contextless" (i: u16) -> u16 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a big endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    from_le_u32 ¶

    from_le_u32 :: proc "contextless" (i: u32) -> u32 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a big endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    from_le_u64 ¶

    from_le_u64 :: proc "contextless" (i: u64) -> u64 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a big endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    from_le_u8 ¶

    from_le_u8 :: proc "contextless" (i: u8) -> u8 {…}
     

    Returns unsigned integer i

    NOTE: A byte has no endianness, so from_le_u8 exists to be complementary to from_le_*.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i

    from_le_uint ¶

    from_le_uint :: proc "contextless" (i: uint) -> uint {…}
     

    Returns unsigned integer i, byte-swapped if we're on a big endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    is_power_of_two_i16 ¶

    is_power_of_two_i16 :: proc "contextless" (i: i16) -> (is_pot: bool) {…}
     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    Related Procedure Groups

    is_power_of_two_i32 ¶

    is_power_of_two_i32 :: proc "contextless" (i: i32) -> (is_pot: bool) {…}
     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    Related Procedure Groups

    is_power_of_two_i64 ¶

    is_power_of_two_i64 :: proc "contextless" (i: i64) -> (is_pot: bool) {…}
     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    Related Procedure Groups

    is_power_of_two_i8 ¶

    is_power_of_two_i8 :: proc "contextless" (i: i8) -> (is_pot: bool) {…}
     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    Related Procedure Groups

    is_power_of_two_int ¶

    is_power_of_two_int :: proc "contextless" (i: int) -> (is_pot: bool) {…}
     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    Related Procedure Groups

    is_power_of_two_u16 ¶

    is_power_of_two_u16 :: proc "contextless" (i: u16) -> (is_pot: bool) {…}
     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    Related Procedure Groups

    is_power_of_two_u32 ¶

    is_power_of_two_u32 :: proc "contextless" (i: u32) -> (is_pot: bool) {…}
     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    Related Procedure Groups

    is_power_of_two_u64 ¶

    is_power_of_two_u64 :: proc "contextless" (i: u64) -> (is_pot: bool) {…}
     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    Related Procedure Groups

    is_power_of_two_u8 ¶

    is_power_of_two_u8 :: proc "contextless" (i: u8) -> (is_pot: bool) {…}
     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    Related Procedure Groups

    is_power_of_two_uint ¶

    is_power_of_two_uint :: proc "contextless" (i: uint) -> (is_pot: bool) {…}
     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    Related Procedure Groups

    leading_zeros ¶

    leading_zeros :: intrinsics.count_leading_zeros
    leading_zeros :: proc(x: $T) -> T where type_is_integer(T) || type_is_simd_vector(T) {…}
     

    Counts the number of leading unset bits (0s) until a set bit (1) is seen or all bits have been counted.

    len_u16 ¶

    len_u16 :: proc "contextless" (x: u16) -> (n: int) {…}
     

    returns the minimum number of bits required to represent x

    Related Procedure Groups

    len_u32 ¶

    len_u32 :: proc "contextless" (x: u32) -> (n: int) {…}
     

    returns the minimum number of bits required to represent x

    Related Procedure Groups

    len_u64 ¶

    len_u64 :: proc "contextless" (x: u64) -> (n: int) {…}
     

    returns the minimum number of bits required to represent x

    Related Procedure Groups

    len_u8 ¶

    len_u8 :: proc "contextless" (x: u8) -> int {…}
     

    returns the minimum number of bits required to represent x

    Related Procedure Groups

    len_uint ¶

    len_uint :: proc "contextless" (x: uint) -> (n: int) {…}
     

    returns the minimum number of bits required to represent x

    Related Procedure Groups

    log2 ¶

    log2 :: proc "contextless" (x: $T) -> (res: $T) {…}
     

    Returns the base-2 logarithm of an unsigned integer x

    Another way to say this is that log2(x) is the position of its leading 1 bit.

    NOTE: This is ill-defined for 0 as it has no 1 bits, and log2(0) will return max(T).

    Inputs:
    x: The unsigned integer

    Returns:
    res: The base-2 logarithm of x

    Example:
    import "core:fmt"
    import "core:math/bits"
    
    log2_example :: proc() {
    	for i in u8(1)..=8 {
    		fmt.printfln("{0} ({0:4b}): {1}", i, bits.log2(i))
    	}
    	assert(bits.log2(  u8(0)) == max(u8))
    	assert(bits.log2( u16(0)) == max(u16))
    	assert(bits.log2( u32(0)) == max(u32))
    	assert(bits.log2( u64(0)) == max(u64))
    	assert(bits.log2(u128(0)) == max(u128))
    }
    
    Output:
    1 (0001): 0
    2 (0010): 1
    3 (0011): 1
    4 (0100): 2
    5 (0101): 2
    6 (0110): 2
    7 (0111): 2
    8 (1000): 3
    

    mul_u32 ¶

    mul_u32 :: proc "contextless" (x, y: u32) -> (hi, lo: u32) {…}
     

    Multiply two words and return the result in high and low word

    Inputs:
    x: The unsigned integer y: Another unsigned integer

    Returns:
    hi: The result's high word lo: The result's low word

    Related Procedure Groups

    mul_u64 ¶

    mul_u64 :: proc "contextless" (x, y: u64) -> (hi, lo: u64) {…}
     

    Multiply two words and return the result in high and low word

    Inputs:
    x: The unsigned integer y: Another unsigned integer

    Returns:
    hi: The result's high word lo: The result's low word

    Related Procedure Groups

    mul_uint ¶

    mul_uint :: proc "contextless" (x, y: uint) -> (hi, lo: uint) {…}
     

    Multiply two words and return the result in high and low word

    Inputs:
    x: The unsigned integer y: Another unsigned integer

    Returns:
    hi: The result's high word lo: The result's low word

    Related Procedure Groups

    overflowing_add ¶

    overflowing_add :: intrinsics.overflow_add
    overflowing_add :: proc(lhs, rhs: $T) -> (T, bool) where type_is_integer(T) #optional_ok {…}
     

    Performs an "add" operation with an overflow check. The second return value will be true if an overflow occurs.

    overflowing_mul ¶

    overflowing_mul :: intrinsics.overflow_mul
    overflowing_mul :: proc(lhs, rhs: $T) -> (T, bool) where type_is_integer(T) #optional_ok {…}
     

    Performs a "multiply" operation with an overflow check. The second return value will be true if an overflow occurs.

    overflowing_sub ¶

    overflowing_sub :: intrinsics.overflow_sub
    overflowing_sub :: proc(lhs, rhs: $T) -> (T, bool) where type_is_integer(T) #optional_ok {…}
     

    Performs a "subtract" operation with an overflow check. The second return value will be true if an overflow occurs.

    reverse_bits ¶

    reverse_bits :: intrinsics.reverse_bits
    reverse_bits :: proc(x: $T) -> T where type_is_integer(T) || type_is_simd_vector(T) {…}
     

    Reverses the bits from ascending order to descending order e.g. 0b01110101 -> 0b10101110

    rotate_left ¶

    rotate_left :: proc "contextless" (x: uint, k: int) -> uint {…}
     

    Returns unsigned integer x rotated left by k bits

    Can be thought of as a bit shift in which the leading bits are shifted back in on the bottom, rather than dropped.

    This is equivalent to the ROL CPU instruction.

    Inputs:
    x: The unsigned integer k: Number of bits to rotate left by

    Returns:
    res: x rotated left by k bits

    rotate_left16 ¶

    rotate_left16 :: proc "contextless" (x: u16, k: int) -> u16 {…}
     

    Returns unsigned integer x rotated left by k bits

    Can be thought of as a bit shift in which the leading bits are shifted back in on the bottom, rather than dropped.

    This is equivalent to the ROL CPU instruction.

    Inputs:
    x: The unsigned integer k: Number of bits to rotate left by

    Returns:
    res: x rotated left by k bits

    rotate_left32 ¶

    rotate_left32 :: proc "contextless" (x: u32, k: int) -> u32 {…}
     

    Returns unsigned integer x rotated left by k bits

    Can be thought of as a bit shift in which the leading bits are shifted back in on the bottom, rather than dropped.

    This is equivalent to the ROL CPU instruction.

    Inputs:
    x: The unsigned integer k: Number of bits to rotate left by

    Returns:
    res: x rotated left by k bits

    rotate_left64 ¶

    rotate_left64 :: proc "contextless" (x: u64, k: int) -> u64 {…}
     

    Returns unsigned integer x rotated left by k bits

    Can be thought of as a bit shift in which the leading bits are shifted back in on the bottom, rather than dropped.

    This is equivalent to the ROL CPU instruction.

    Inputs:
    x: The unsigned integer k: Number of bits to rotate left by

    Returns:
    res: x rotated left by k bits

    rotate_left8 ¶

    rotate_left8 :: proc "contextless" (x: u8, k: int) -> u8 {…}
     

    Returns unsigned integer x rotated left by k bits

    Can be thought of as a bit shift in which the leading bits are shifted back in on the bottom, rather than dropped.

    This is equivalent to the ROL CPU instruction.

    Inputs:
    x: The unsigned integer k: Number of bits to rotate left by

    Returns:
    res: x rotated left by k bits

    Example:
    import "core:fmt"
    import "core:math/bits"
    
    rotate_left8_example :: proc() {
    	x := u8(13)
    	for k in 0..<8 {
    		fmt.printfln("{0:8b}: {1}", bits.rotate_left8(x, k), k)
    	}
    }
    
    Output:
    00001101: 0
    00011010: 1
    00110100: 2
    01101000: 3
    11010000: 4
    10100001: 5
    01000011: 6
    10000110: 7
    

    sub_u32 ¶

    sub_u32 :: proc "contextless" (x, y, borrow: u32) -> (diff, borrow_out: u32) {…}
     

    Subtract with borrow

    Inputs:
    x: The unsigned integer y: Another unsigned integer borrow: Borrow in

    Returns:
    diff: The difference borrow_out: Borrow out

    Related Procedure Groups

    sub_u64 ¶

    sub_u64 :: proc "contextless" (x, y, borrow: u64) -> (diff, borrow_out: u64) {…}
     

    Subtract with borrow

    Inputs:
    x: The unsigned integer y: Another unsigned integer borrow: Borrow in

    Returns:
    diff: The difference borrow_out: Borrow out

    Related Procedure Groups

    sub_uint ¶

    sub_uint :: proc "contextless" (x, y, borrow: uint) -> (diff, borrow_out: uint) {…}
     

    Subtract with borrow

    Inputs:
    x: The unsigned integer y: Another unsigned integer borrow: Borrow in

    Returns:
    diff: The difference borrow_out: Borrow out

    Related Procedure Groups

    to_be_u16 ¶

    to_be_u16 :: proc "contextless" (i: u16) -> u16 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a little endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    to_be_u32 ¶

    to_be_u32 :: proc "contextless" (i: u32) -> u32 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a little endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    to_be_u64 ¶

    to_be_u64 :: proc "contextless" (i: u64) -> u64 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a little endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    to_be_u8 ¶

    to_be_u8 :: proc "contextless" (i: u8) -> u8 {…}
     

    Returns unsigned integer i

    NOTE: A byte has no endianness, so to_be_u8 exists to be complementary to to_be_*.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i

    to_be_uint ¶

    to_be_uint :: proc "contextless" (i: uint) -> uint {…}
     

    Returns unsigned integer i, byte-swapped if we're on a little endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    to_le_u16 ¶

    to_le_u16 :: proc "contextless" (i: u16) -> u16 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a big endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    to_le_u32 ¶

    to_le_u32 :: proc "contextless" (i: u32) -> u32 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a big endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    to_le_u64 ¶

    to_le_u64 :: proc "contextless" (i: u64) -> u64 {…}
     

    Returns unsigned integer i, byte-swapped if we're on a big endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    to_le_u8 ¶

    to_le_u8 :: proc "contextless" (i: u8) -> u8 {…}
     

    Returns unsigned integer i

    NOTE: A byte has no endianness, so to_le_u8 exists to be complementary to to_le_*.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i

    to_le_uint ¶

    to_le_uint :: proc "contextless" (i: uint) -> uint {…}
     

    Returns unsigned integer i, byte-swapped if we're on a big endian target.

    Inputs:
    i: The unsigned integer

    Returns:
    res: i, optionally byte-swapped

    trailing_zeros ¶

    trailing_zeros :: intrinsics.count_trailing_zeros
    trailing_zeros :: proc(x: $T) -> T where type_is_integer(T) || type_is_simd_vector(T) {…}
     

    Counts the number of trailing unset bits (0s) until a set bit (1) is seen or all bits have been counted.

    Procedure Groups

    add ¶

    add :: proc{
    	add_u32,
    	add_u64,
    	add_uint,
    }
    
     

    Add with carry

    Inputs:
    x: The unsigned integer y: Another unsigned integer carry: Carry in

    Returns:
    sum: The sum carry_out: Carry out

    bitfield_extract ¶

     

    Extracts bits from an integer

    Inputs:
    value: Integer offset: Offset (counting from LSB) at which to extract bits: Number of bits to extract

    Returns:
    res: bits bits starting at offset offset

    bitfield_insert ¶

     

    Insert a subset of bits from one integer into another integer

    Copies bits number of insert's lower bits to base at offset.

    Inputs:
    base: Original integer to insert bits into insert: Integer to copy bits from offset: Bit offset in base at which to place insert's bits bits: Number of bits to copy

    Returns:
    res: base with bits bits at offset replaced with insert's

    div ¶

    div :: proc{
    	div_u32,
    	div_u64,
    	div_uint,
    }
    
     

    Divide an unsigned integer (in two words) by a divisor

    Inputs:
    hi: High word of input lo: Low word of input y: Divisor

    Returns:
    quo: Quotient rem: Remainder

    is_power_of_two ¶

     

    Checks whether an unsigned number is a power of two

    Inputs:
    i: Unsigned number

    Returns:
    is_pot: true if i is a power of two, false otherwise

    len ¶

    len :: proc{
    	len_u8,
    	len_u16,
    	len_u32,
    	len_u64,
    	len_uint,
    }
    
     

    returns the minimum number of bits required to represent x

    mul ¶

    mul :: proc{
    	mul_u32,
    	mul_u64,
    	mul_uint,
    }
    
     

    Multiply two words and return the result in high and low word

    Inputs:
    x: The unsigned integer y: Another unsigned integer

    Returns:
    hi: The result's high word lo: The result's low word

    sub ¶

    sub :: proc{
    	sub_u32,
    	sub_u64,
    	sub_uint,
    }
    
     

    Subtract with borrow

    Inputs:
    x: The unsigned integer y: Another unsigned integer borrow: Borrow in

    Returns:
    diff: The difference borrow_out: Borrow out

    Source Files

    Generation Information

    Generated with odin version dev-2025-10 (vendor "odin") Windows_amd64 @ 2025-10-27 21:13:15.341999200 +0000 UTC