Rust program to demonstrate the bitwise left-shift (<<) operator

Here, we are going to demonstrate the bitwise left-shift (<<) operator in Rust programming language.
Submitted by Nidhi, on September 23, 2021

Problem Solution:

Left shift (<<): The left shift operator (<<) shifts the first operand the specified number of bits to the left. Here, we will perform a bitwise left-shift (<<) operation between two variables and print the result.

Program/Source Code:

The source code to demonstrate the bitwise left-shift (<<) operator is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// bitwise left-shift "<<" operator

fn main() {
    let mut num1:i32 = 4;
    let mut num2:i32 = 3;
    let mut res:i32  = 0;

    res = num1 << num2;

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

Output:

4 << 3 = 32

Explanation:

Here, we created three integer variables num1, num2, res that are initialized with 4, 3, 0 respectively. Then we performed a bitwise left shift operation and printed the result.

Evolution of expression:

res = 4 << 3
res = 4 * (23)
res = 4 * 8
res = 32

Rust Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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