Finding the power of a number

how to find power of number in rust language like 2-raise-32

You can use pow method of each number type to compute an integral power of an integer. e.g. 232 can be computed by 2u64.pow(32). Note that i32 or u32 cannot represent 232, So you have to use one of the bigger types.

A gotcha is you cannot write 2.pow(5) because the type of 2 is not determined (can be u32, u64 or other types). You have to specify number type by either using suffix like 2u32.pow(5) or specifying the type of a variable let x: u32 = 5; x.pow(5).

I don't understand what you mean (because I'm not good at English), but if you want log_2(32), then you can use u64::leading_zeros().

fn main() {
    let log2_32 = (64 - 1) - 32u64.leading_zeros();
    println!("log_2 32 = {}", log2_32); // => 5

    let log2_100 = (64 - 1) - 100u64.leading_zeros();
    println!("log_2 100 = {}", log2_100); // => 6
}

If you want to round up the result, use u64::next_power_of_two() and then use .leading_zeros().

fn main() {
    let log2_32 = (64 - 1) - 32u64.next_power_of_two().leading_zeros();
    println!("ceil(log_2 32) = {}", log2_32); // => 5

    let log2_100 = (64 - 1) - 100u64.next_power_of_two().leading_zeros();
    println!("ceil(log_2 100) = {}", log2_100); // => 7
}

I'll add that if you're explicitly looking for a power of two, you can just use the bit shift operator 1 << 32.