Previously on jonathanpike.net, I gave a rundown of most of the operators in Ruby, with the promise that I would cover certain omissions. The day has come, my friends, to addressâ€¦ bitwise operators!
Bitwise operators are not hard to understand but theyâ€™re also not intuitive. The key to understanding them is realizing that they work on a bit level. Bitwise operators work because Ruby stores integers in memory as a sequence of bits, which allows bitwise operators to manipulate these bits. Bitwise operators only work with Integers.
Binary Refresher
Understanding bitwise operators requires a basic understanding of binary numbers. Letâ€™s use this variable as an example: a = 21
. 21
is an integer, which is immediately recognizable to us (given our decimal/base 10 number system). Computers, on the other hand, work in binary/base 2. We can use the Integer#to_s
method to convert a base 10 number to a base 2 number as follows:
If youâ€™ve never been introduced to binary numbers, that may look a little confusing. A simple way of thinking about binary numbers is in terms of â€ścolumnsâ€ť: each column represents a certain number and each column can be either a 0 or a 1. The columns go like this (from right to left): one  two  four  eight  sixteen  thirtytwo  sixtyfour â€¦ etc
. If the bit in the column is 1, add the value of the column to the total, and if the bit in the column is 0, donâ€™t add the value of the column to the total.
In our example, the ones column is 1, so we add 1 to our total. The twos column is 0, so we skip it. The fours column is 1, bringing our total to 5. Skip the eights column, which leaves us with the sixteens column. 16 + 5 = 21.
Bonus Content: hereâ€™s a super easy way to convert binary numbers to decimal numbers in your head without having to think of them in columns!
Back to Bitwise Operators
There are 6 bitwise operators in ruby, almost of which are binary operators (operators that work with 2 operands), as follows:
&
 the AND operator

 the OR operator
^
 the XOR (exclusive or) operator
~
 the NOT operator â€“ this is the only unary operator in the bunch
>>
 the right shift operator
<<
 the left shift operator
A few of these look exactly like the boolean operators that I addressed last time and it turns out they work in a similar way.
AND
The &
operator will look at the binary representation of both numbers and will return 1
when both numbers have a 1
bit in the same column. Weâ€™ll use our variable a
as defined before, and weâ€™ll define another variable to use as an example:
Since the only column that a
and b
had in common was the sixteens column, that is the only column that returned 1
.
OR
The 
operator works in a similar way to the &
operator, but will set any bit that is 1
in either number to 1
in the return value:
XOR
The ^
operator is the opposite to the &
, and will only set a bit as 1
if that bit is 1
in only one of the two compared numbers:
NOT
The ~
operator does exactly what you think it does: it flips all of the bits of the number (if they were 1
, they are now 0
). Strangely, the Integer#to_s
method produces some odd output:
Whatâ€™s going on here? Shouldnâ€™t it be 01010
? The answer comes from how binary numbers handle negatives, and how the Integer#to_s
method deals with negative binary numbers.
Binary numbers donâ€™t natively have a negative sign to deal with negative numbers. Instead, they are represented through a method called twoâ€™s compliment. A really great explanation of how it works can be seen here^{1}. In a nutshell, to convert to twoâ€™s complement notation, the leftmost bit becomes the sign value (which reduces the range that your binary number represents), all of the bits are flipped (with the bitwise NOT), and you add 1.
Secondly, the Integer#to_s
method does not actually return a true representation of binary numbers when dealing with negatives. Instead, it returns a representation of the number prepended with a negative sign. In this example:
Using the Fixnum#Fix[n]
method, we can see the binary representation of each bit in our number, which shows NOTs true colours:
Â«Â andÂ Â»
The right and left shift operators simply shift an integerâ€™s bits in the direction you specify. It will add 0s if shifting left and remove bits if shifting right, as per how many spaces you tell it to shift. For example:
So What?
All of this is interesting at an academic level, but itâ€™s hard to think of a use for bitwise operators in day to day Ruby programming. Why have a feature that isnâ€™t all that useful? The answer is that while this functionality may be specialized, it does have uses.
Calle Erlandsson wrote an article showing how bitwise operators are useful when dealing with file system permissions. Additionally, this Stack Overflow post shows several realworld uses for bitwise operators.
Credits
I owe a deep debt of gratitude to Calle Erlandssonâ€™s article, Rubyâ€™s Bitwise Operators. The content in this article has its foundation in the understanding that I got from his explanations.

Hiromi K pointed out this additional article on Twoâ€™s Compliment that may be helpful.Â ↩