Q:

Rust program to swap nibbles of a number

belongs to collection: Rust Basic Programs

0

Here, we will create an 8-bit integer number and then we will swap the nibbles of the given number and print the result.

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 swap nibbles of a number is given below. The given program is compiled and executed successfully.

// Rust program to swap nibbles of a number.

fn main()
{
    let mut num:u8 = 0x34;

    println!("Number before swapping nibbles is: {:#02x}", num);
    
    num = (num & 0x0F) << 4 | (num & 0xF0) >> 4;
    
    println!("Number after swapping nibbles is: {:#02x}", num);
}

Output:

Number before swapping nibbles is: 0x34
Number after swapping nibbles is: 0x43

Explanation:

Here, we created an 8-bit integer variable num with an initial value of 0x34. Then we swapped the nibbles of the given number and printed the result.

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 count the total number of trailing... >>
<< Rust program to count the total number of HIGH (1)...