Skip to content

Func

dstz.math.matrix.func

get_ones_indices(n)

Gets the indices of set bits in the binary representation of an integer.

This function is used to map an integer to a subset. In the context of this library, if a Frame of Discernment has N elements, any integer from 0 to 2^N - 1 can represent a subset. The indices of the '1's in the integer's binary form correspond to the indices of the elements in the subset.

Parameters:

Name Type Description Default
n int

A non-negative integer.

required

Returns:

Name Type Description
list

A list of indices where the bits are set to 1.

Example

get_ones_indices(5) # Binary of 5 is 101 [0, 2] get_ones_indices(10) # Binary of 10 is 1010 [1, 3]

Source code in dstz/math/matrix/func.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def get_ones_indices(n):
    """Gets the indices of set bits in the binary representation of an integer.

    This function is used to map an integer to a subset. In the context of
    this library, if a Frame of Discernment has N elements, any integer from
    0 to 2^N - 1 can represent a subset. The indices of the '1's in the
    integer's binary form correspond to the indices of the elements in the
    subset.

    Args:
        n (int): A non-negative integer.

    Returns:
        list: A list of indices where the bits are set to 1.

    Example:
        >>> get_ones_indices(5)  # Binary of 5 is 101
        [0, 2]
        >>> get_ones_indices(10) # Binary of 10 is 1010
        [1, 3]
    """
    indices = []
    index = 0
    while n:
        if n & 1:
            indices.append(index)
        n >>= 1
        index += 1
    return indices