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.

Yes.

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

Example:

8.even?
# true
5.even?
# false

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

Example:

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.

Example:

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"
end
if n % 5 == 0
puts "Buzz"
end

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

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.

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

Here’s a code example:

class Bitmask
def initialize
@value = 0
end
def set(bit)
@value |= bit
end
def clear(bit)
@value &= ~bit
end
def check(bit)
(@value & bit) == bit
end
def to_binary
@value.to_s(2)
end
end
bit = Bitmask.new

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.

Example:

class Bitmask
ENGINES_ENABLED = 1
CAPTAIN_ABOARD = 2
SHIELDS_UP = 4
# ... rest of code here
end
bit = Bitmask.new
bit.set(Bitmask::ENGINES_ENABLED)
bit.check(Bitmask::ENGINES_ENABLED)

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

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

1100001

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)

Examples:

5 ** 2
# 25
-10.abs
# 10
300.digits
# [0, 0, 3]

Math in Ruby Video

Summary

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!

Related

4 comments

bedawang says
4 years ago

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