ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.