Q:

Rust program to count the total number of trailing zeros

belongs to collection: Rust Basic Programs

0

Here, we will create a 16-bit integer number and then we will count the total number of trailing zeros and print the result.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to count the total number of trailing zeros is given below. The given program is compiled and executed successfully.

// Rust program to count the 
// total number of trailing zeros

fn main() {
    let mut num:i16 = 12;
    let mut val:i16 = 0;
    let mut tmp:i16 = 0;
    
    while val<16
    {
        tmp = num & (1<<val);
        if tmp>0
        {
            break;    
        }
        
        val = val + 1;
    }

    println!("Total number of trailing 0's are: {}",val);
}

Output:

Total number of trailing 0's are: 2

Explanation:

Here, we created a 16-bit integer variable num with an initial value of 12. Then we counted the total number of trailing zeros using bitwise operators and printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to check a given number EVEN or ODD u... >>
<< Rust program to swap nibbles of a number...