Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
bitwise
or
communitycreator

How to perform a bitwise OR operation in Ruby

okwudili onyejiaku

Overview

The bitwise OR operation is carried out with the | operator in Ruby. It first converts its operands to their equivalent binary representations. Then it adds the binaries and returns the result in the form of a decimal.

For instance, the expression 2 | 1 can be evaluated by adding the bits, which are 10 and 01. When they are added, the result becomes 11. The equivalent decimal is 3. This is how the bitwise OR operation works.

Syntax

Let's look at the syntax for this:

number1 | number2
Syntax for bitwise OR operation in Ruby

Parameters

number1 and number2 are the numbers we want to get the result of by adding their bits together.

Return value

A decimal is returned. This decimal is the decimal representation of adding the bits of number1 and number2.

Code example

Let's look at an example of the bitwise OR operation below.

puts 2 | 1
puts 10 | 5
puts 200 | 2
puts 120 | 3
Bitwise OR operation in Ruby

Explanation

In the code above, we perform a bitwise OR operation on some numbers and print the results to the console.

RELATED TAGS

ruby
bitwise
or
communitycreator
RELATED COURSES

View all Courses

Keep Exploring