Q:

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

belongs to collection: Rust Basic Programs

0

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).

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 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.

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 print the hexadecimal number of th... >>
<< Rust program to get the minimum number of bits to ...