Ruby program to demonstrate the bitwise XOR (^) operator

Ruby Example: Write a program to demonstrate the bitwise XOR (^) operator.
Submitted by Nidhi, on December 03, 2021

Problem Solution:

In this program, we are going to demonstrate the bitwise “XOR" (^) operator in the Ruby programming language.

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.

Program/Source Code:

The source code to demonstrate the Bitwise XOR (^) operator is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate the 
# bitwise XOR (^) operator

res =0

res = num1 ^ num2;
print num1," ^ ", num2," = ",res,"\n"


5 ^ 3 = 6


In the above program, we created three integer variables num1, num2, res that are initialized with 5, 3, 0 respectively. Then we performed a Bitwise XOR operation using the (^) operator and printed the result using the print() function.

Evolution of expression:

res = 5 ^ 3


Ruby Basic Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.