Rust program to check specific bit is HIGH (1) or LOW (0)

Here, we are going to learn how to check specific bit is HIGH (1) or LOW (0) in Rust programming language?
Submitted by Nidhi, on September 24, 2021

Problem Solution:

Here, we will create an 8-bit integer number and then we will check 3rd bit of the number is HIGH (1) or LOW (0).

Program/Source Code:

The source code to check specific bit is HIGH (1) or LOW (0) is given below. The given program is compiled and executed successfully.

// Rust program to check specific 
// bit is HIGH or LOW

fn main()
{
    let num:i8 = 8;
    let mut res:i8 = 0;
    
    res = num & (1<<3);
    if res>0
    {
        println!("3rd bit is HIGH");
    }
    else
    {
        println!("3rd bit is LOW");
    }
}

Output:

3rd bit is HIGH

Explanation:

Here, we created an 8-bit integer variable num with an initial value of 8. Then we checked 3rd bit of the number is HIGH (1) or LOW (0) and printed the appropriate message.

Rust Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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