Home »
Ruby »
Ruby Programs
Ruby program to demonstrate the bitwise right-shift (>>) operator
Ruby Example: Write a program to demonstrate the bitwise right-shift (>>) operator.
Submitted by Nidhi, on December 03, 2021
Problem Solution:
In this program, we are going to demonstrate the bitwise right-shift (>>) operator in the Ruby programming language.
Right shift (>>): The right shift operator (>>) shifts the first operand the specified number of bits to the right. Here, we will perform a bitwise right-shift (>>) operation between two variables and print the result.
Program/Source Code:
The source code to demonstrate the bitwise right-shift ">>" operator is given below. The given program is compiled and executed successfully.
# Ruby program to demonstrate the
# bitwise right-shift ">>" operator
num1=16
num2=2
res =0
res = num1 >> num2;
print num1," >> ", num2," = ",res
Output:
16 >> 2 = 4
Explanation:
In the above program, we created three integer variables num1, num2, res that are initialized with 16, 2, 0 respectively. Then we performed a bitwise right shift operation using the ">>" operator and printed the result using the print() function.
Evolution of expression:
res = 16 >> 2
res = 16 / 22
res = 16 / 4
res = 4
Ruby Basic Programs »