ADVERTISEMENT
ADVERTISEMENT

Ruby program to demonstrate the bitwise OR (|) operator

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

Problem Solution:

In this program, we are going to demonstrate the Bitwise OR (|) Operator in the Ruby programming language.

Here, Bitwise operator performs a Bitwise OR operation on binary numbers and prints the resulting decimal number.

Program/Source Code:

The source code to demonstrate the bitwise OR "|" operator is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate the 
# bitwise OR "|" operator

num1=5
num2=3
res =0

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

num1=7
num2=4
res = num1 | num2;
print num1," | ", num2," = ",res

Output:

5 | 3 = 7
7 | 4 = 7

Explanation:

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 "OR" operation using the (|) operator and printed the result using the print() function.

Evolution of expression:

res = 5 | 3
0101
0011
=====
0111

res = 7

res = 7 & 4
0111
0100
=====
0111

res = 7

Ruby Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.