Rust program to count the total number of trailing zeros

Here, we are going to learn how to count the total number of trailing zeros in Rust programming language?
Submitted by Nidhi, on September 24, 2021

Problem Solution:

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

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.

Rust Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.