Home »
Ruby Tutorial »
Ruby Programs
Ruby program to demonstrate the bitwise left-shift (<<) operator
Last Updated : December 15, 2025
Problem Solution
In this program, we are going to demonstrate the bitwise left-shift (<<) operator in the Ruby programming language.
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.
# Ruby program to demonstrate the
# bitwise left-shift "<<" operator
num1=4
num2=3
res =0
res = num1 << num2;
print num1," << ", num2," = ",res
Output
4 << 3 = 32
Explanation
In the above program, we created three integer variables num1, num2, res that are initialized with 4, 3, 0 respectively. Then we performed a bitwise left shift operation using the "<<" operator and printed the result using the print() function.
Evolution of expression:
res = 4 << 3
res = 4 * (23)
res = 4 * 8
res = 32
Ruby Basic Programs »
Advertisement
Advertisement