Q:

Rust program to convert a decimal number to a binary number

belongs to collection: Rust Basic Programs

0

Here, we will create a 16-bit integer number and then print the corresponding binary number.

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 convert a decimal number to a binary number is given below. The given program is compiled and executed successfully.

// Rust program to convert decimal number 
// to binary number

fn main() {
    let mut num:i16 = 14;
    let mut cnt:i16 = 15;
    let mut tmp:i16 = 0;
    
    println!("Decimal Number: {}",num);
    print!("Binary Number: ");
    while cnt>=0
    {
        tmp = num & (1<<cnt);
        if tmp>0
        {
            print!("1");
        }
        else
        {
            print!("0");
        }
        cnt = cnt - 1;
    }
}

Output:

Decimal Number: 14
Binary Number: 0000000000001110

Explanation:

Here, we created an integer variable num with an initial value of 14. Then we used bitwise operators to print the corresponding binary number.

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 get the minimum number of bits to ... >>
<< Rust program to clear specific bit using bitwise o...