Ruby Bitwise Operators

An operator is bitwise when instead of treating integers as whole numbers, it treats them as a sequence of bits.

This is hugely used to apply a mask to an integer. For example, to verify the answers of an MCQ test.

So let’s have a look at how the bitwise operators work in Ruby.

Base 10 to Base 2

The base 10 is the common base that we use everyday.

The base 2 is the base used by your computer to make operations. This base works with 2 values that are 0 and 1.

Each bit has a weight that is a multiple of 2. each value is assigned from the right to the left.

  • the first bit (from the right) has a value of 1
  • the second one a value of 2
  • the third one a value of 4
  • etc..

If the bit is set to 1 then the value attached to this bit is counted to retrieve the number.

Let’s have a look at the following example to see how to convert a number in base 2.

Here, we convert the number 34 in base 2

128 64 32 16  8 4 2 1
| | | | | | | |
0 0 1 0 0 0 1 0

So, to convert a number in base 2 we move from the left to the right and we apply the following rule:

  • Does 128 is included in 34 ? No
  • Does 64 is included in 34 ? No
  • Does 32 is included in 34 ? Yes: 34 — 32 = 2
  • Does 16 is included in 2 ? No
  • Does 8 is included in 2 ? No
  • Does 4 is included in 2 ? No
  • Does 2 is included in 2 ? Yes: 2 — 2 = 0

So the bits attached to 32 and 2 will be set to 1.

Base 2

As we’ve seen in introduction, a bitwise operator treats integers as a sequence of bits — In base 2 instead of base 10.

So, how to see an integer as a sequence of bits ?

$> 4.to_s(2)
=> "100"

The Integer#to_s(base = 10) can take an argument that is the base where the Integer will be returned. By default, it’s the base 10 — the common base.

The to_s(2) doesn’t return the ahead zeros. It’ll return a String that contains the sequence of bits that starts with the first 1 encountered from the left to the right.

Bitwise AND (&)

The Bitwise AND operator applies a & operation on each bit of the sequence.

So let’s detail the following operation 7 & 5

7 in Base 2: 0000 0111
5 in Base 2: 0000 0101
  128 64 32 16 8 4 2 1
----------------------
7: 0 0 0 0 0 1 1 1
& & & & & & & &
5: 0 0 0 0 0 1 0 1
-----------------------
5: 0 0 0 0 0 1 0 1

So the result of the operation 7 & 5 is 5.

Let’s make this operation in Ruby

$> 7 & 5
=> 5
$> (7 & 5).to_s(2)
=> "101"

Bitwise OR

The Bitwise OR operator applies a | operation on each bit of the sequence

So let’s detail the following operation 7 | 5

7 in Base 2: 0000 0111
5 in Base 2: 0000 0101
  128 64 32 16 8 4 2 1
----------------------
7: 0 0 0 0 0 1 1 1
| | | | | | | |
5: 0 0 0 0 0 1 0 1
----------------------
7: 0 0 0 0 0 1 1 1

So the result of the operation 7 | 5 is 7.

Let’s make this operation in Ruby

$> 7 | 5
=> 7
$> (7 | 5).to_s(2)
=> "111"

Bitwise LEFT SHIFT

The LEFT SHIFT operator << shifts each bit of a number to the left by n positions.

So let’s detail the following operation 7 << 2

7 in Base 2: 0000 0111
   128 64 32 16  8 4 2 1
----------------------
7: 0 0 0 0 0 1 1 1
shift each bit to the left by 2 positions
----------------------
28: 0 0 0 1 1 1 0 0

So the result of the operation 7 << 2is 28.

Let’s make this operation in Ruby

$> 7 << 2
=> 28
$> (7 << 2).to_s(2)
=> "11100"

Bitwise RIGHT SHIFT

The RIGHT SHIFT operator >> shifts each bit of a number to the right by n positions.

So let’s detail the following operation 40 >> 2

40 in Base 2: 0010 1000
   128 64 32 16  8 4 2 1
----------------------
7: 0 0 1 0 1 0 0 0
shift each bit to the right by 2 positions
----------------------
10: 0 0 0 0 1 0 1 0

So the result of the operation 40 >> 2is 10.

Let’s make this operation in Ruby

$> 40 >> 2
=> 10
$> (40 >> 2).to_s(2)
=> "1010"

Conclusion

Bitwise operators are not commonly used in Rails but can be pretty handy when we’ve a “multiple choices” feature as MCQ test, configs, options, etc..

Note that there is 2 other bitwise operators that we didn’t cover in this article: the bitwise exclusive or and the bitwise not operators. I’ll probably cover them in another article.

Voilà !

May I have your attention please 🎤🎤

Feel free to subscribe here: www.rubycademy.com


Thank you for taking the time to read this post :-)

Feel free to 👏 and share this Medium post if it has been useful for you.

Here is a link to my last medium post: Ruby: class_eval vs module_eval.