Convert bit to number?

This is more of a general C++ question however I can’t find a simple answer on google (they all involve huge code loop programs).

To convert a number to bits (1, 2, 3, 4, 5, 6, becomes 1, 2, 4, 8, 16, 32)

bits = FMath::Pow(2, number - 1);

But how can I reverse that, so 1, 2, 4, 8, 16, 32, becomes 1, 2, 3, 4, 5, 6?

number = FMath::???(bits);

Thanks.

P.s. I’ve been using it forever but have never been 100% sure, is bits the right word to describe dealing with 2, 4, 8, 16, 32, &&, ||, etc in programming?

If you know the number will always be a power of 2, you can just keep dividing it by 2 and count the number of times it takes to reduce it to 1.

int bitToNumber(int bits) {

    int count = 1;
    while(bits > 1)
    {
        bits /= 2;
        ++count;
    }
    return count;
    
}

Ah yeah, I was hoping it could also be done in a simple 1-line math function too. I’ll use that for now and mark it as answered if another 1-line equation doesn’t pop up.

Thanks.

You can just use FMath::FloorLog2(uint32) or FMath::CeilLogTwo(uint32)

Excellent thanks!