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.
Let's look at the syntax for this:
number1 | number2
number1
and number2
are the numbers we want to get the result of by adding their bits together.
A decimal is returned. This decimal is the decimal representation of adding the bits of number1
and number2
.
Let's look at an example of the bitwise OR operation below.
puts 2 | 1 puts 10 | 5 puts 200 | 2 puts 120 | 3
In the code above, we perform a bitwise OR operation on some numbers and print the results to the console.
RELATED TAGS
CONTRIBUTOR
View all Courses