Math With Ruby: Modulo Operator, Binary & More

Do you need to know math to become a good programmer?

It depends!

If you’re just going to be writing CRUD apps all day then you probably don’t need to know much math, if any.

But if you want to do more interesting things, like solving coding challenges & be prepared for coding interviews then learning a few basic concepts is helpful.

Today you’re going to learn about:

  • The modulo operator (%)
  • Number systems
  • Bitmasking

You’ll learn how to apply these concepts in Ruby, so this is going to be a practical guide.

Let’s do this!

Ruby Modulo Operator

The Ruby modulo operator looks like this:


Yes, just like the percentage symbol.

What does it do?

The modulo operator gives you the remaining of a division. This can be used for things like checking if a number is even or odd.


In Ruby, we have the even?/odd? methods.


# true

# false

But if you want to check if a number is divisible by 3, then you have to use the modulo operator.


9 % 3 == 0
# true

Let’s explore more uses!

Practical Uses For The Modulo Operator

You can use the modulo to check if a number is divisible by another.

A number is divisible if the remaining is 0.


The classic “FizzBuzz” coding challenge wants you to find out if a number is divisible by 3 or 5.

if n % 3 == 0
  puts "Fizz"

if n % 5 == 0
  puts "Buzz"

You can use the modulo operator to do things every Nth time.

Like this:

(1..10).select { |n| n % 2 == 0 }
# [2, 4, 6, 8, 10]

Or you can use the step method:

# [2, 4, 6, 8, 10]

Using The Divmod Method

Another use for the modulo operator is to convert minutes to hours + remaining minutes.


We have 90 minutes, which is the same as 1 hour & 30 minutes.

hours, minutes = 90.divmod(60)
# [1, 30]

Notice the divmod method:

It does division & modulo (the remaining of the division) at the same time.

Very helpful!

Understanding Number Systems

A number system is a way to represent numbers.

In your daily use of numbers you use the decimal system.


A number system is composed of a set of numbers & sometimes characters too.

For example:

The hexadecimal system uses 16 symbols in total.


Here’s a table of common numeric systems:

NameSymbol CountSymbols

You can convert between number systems in Ruby with the to_s method.

Here’s how to convert from decimal (9) to binary (1001):

# "1001"

You can use the to_i method on a string to convert back into an integer.

So if you want to go from hexadecimal (ff) to decimal (255) you can do this:

# 255

Where 16 is the “symbol count” or base for the number.

What is Bitmasking?

You probably don’t wake up every day thinking…

“Hey! I need to pack a lot of boolean values into as little space as possible.”

But if someday you need to do that…

A great technique that can help you is “bitmasking”.

With bitmasking, you can pack a lot of boolean values into a single integer value.

How is that possible?

By using the individual bits that the number is made of.

Because a boolean value can be represented by a single bit, and an integer value has 64 bits, we can pack up to 64 boolean values into a single number.

We are going to use bitwise operators.

Here’s a table:

XOR (Exclusive OR)^Toggle Bit
AND&Check Bit
NOT~Clear Bit
OR|Set Bit

Bitwise operators work at the BIT level & that’s exactly what we want.

Here’s a code example:

class Bitmask
  def initialize
    @value = 0

  def set(bit)
    @value |= bit

  def clear(bit)
    @value &= ~bit

  def check(bit)
    (@value & bit) == bit

  def to_binary

bit =

How to Use BitMasking

Now you can use the set, clear & check methods to work with this data structure. You may also want to define constants to describe what each value means.


class Bitmask
  SHIELDS_UP      = 4

  # ... rest of code here

bit =


Valid values for set include 1 & the powers of 2 (2,4,8,16,32…), this avoids overwriting other bits.

Powers Of Two

If we have 64 + 32 + 1, the stored value will look like this:


Ruby Math Methods

Ruby includes a few built-in math methods that can be helpful.

We already covered divmod, even? & odd?.

Other methods include:

  • ** / pow (exponentiation)
  • gcd (greatest common divisor)
  • abs (absolute value, removes negative sign)
  • round (round to closest integer)
  • floor & ceil (round down / round up)
  • Math.sqrt(n) (square root of n)
  • Math.log2(n) (log2 of n)
  • digits (converts integer into a reverse array of digits)


5 ** 2
# 25

# 10

# [0, 0, 3]

Math in Ruby Video


You’ve learned a few interesting math tricks, like using modulo % to find out the remainder of a division. You can use the remainder to check if a number is divisible by another.

You’ve also learned about number systems, bitmasking & bitwise operators.

Don’t forget to share this post…

And to subscribe to the RubyGuides newsletter if you haven’t yet!

4 thoughts on “Math With Ruby: Modulo Operator, Binary & More”

  1. very good article! learned some new things, as always.
    One remark, isn’t it more convenient to use :

    puts “Fizz” if (n%3).zero?

    instead of

    if n % 3 == 0
      puts "Fizz"

Comments are closed.