Q:

Rust program to demonstrate the bitwise XOR (^) operator

belongs to collection: Rust Basic Programs

0

Bitwise XOR (^): The bitwise XOR operator (^) returns a 1 in each bit position for which the corresponding bits of either but not both operands are 1s. Here, we will perform the bitwise XOR (^) operation between two variables 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 demonstrate the bitwise XOR (^) operator is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// bitwise XOR (^) operator

fn main() {
    let mut num1:i32 = 6;
    let mut num2:i32 = 2;
    let mut res:i32  = 0;

    res = num1 ^ num2;

    println!("{0} ^ {1} = {2}",num1,num2,res);
}

Output:

6 ^ 2 = 4

Explanation:

Here, we created three integer variables num1num2res that are initialized with 6, 2, 0 respectively. Then we performed the bitwise OR operation and printed the result.

Evolution of expression:

res = 6 ^ 2

110
010
====
100

That is 4.

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 swap two numbers using bitwise XOR... >>
<< Rust program to demonstrate the bitwise OR (|) ope...