Lately I’ve been trying to challenge myself by taking a stab at some common coding questions asked in software engineering job interviews. After years of working on apps you would think that this kind of thing comes easy, but software development in the modern age comes with a heavy side of Googling and lookup, so challenging your brain in this way to implement an algorithm or process from just its definition is good practice to keep you sharp.
I had come across a practice coding question which required me to compare integers and binary strings, and I realized that I had forgotten how to convert an integer to binary. Looking it up, the algoritm is pretty simple:
To convert integer to binary, start with the integer in question and divide it by 2 keeping notice of the quotient and the remainder. Continue dividing the quotient by 2 until you get a quotient of zero. Then just write out the remainders in the reverse order.
Angular in Depth
Here’s how I implemented that in ruby.
Basically, we need to keep dividing by 2 and record the remainder, so this sounds to me like a while
loop, or even a recursive function. I’ll implement both
here, and at the end, I’ll show you an even easier method to use with builtin ruby methods. We will only deal with integers >= 0
, and output a string
representation of the integer written in binary. For example, an input of 9
would output "1001"
, and an input of 1337
would output "10100111001"
.
Starting with a simple test case to prove our results are correct:
1 2 3 4 5 6 7 

Running that of course is going to fail:
1 2 

So let’s get to work implementing the while method. We want to take the input n and divide it by 2, but how do we do that while “keeping notice of the quotient and the remainder”? We can
get the quotient by simply running a standard divide by the integer 2, as ruby will always round down. If we divide by a float 2.0, ruby would provide a float return and we could see if the portion
to the right of the decimal was > 0
or not, but I think there is a better way using the mod
operator: %
. This mod operator only returns the remainder as an integer, not as
a fraction. Because we are dividing by 2, the remainder will always be either 1 or 0, and thus the mod will always return either 1 or 0, already conveniently in base2, or binary representation.
1 2 3 4 5 6 7 8 

Line by line, we first initialize an empty array to hold the binary digits. We do a while loop over integer
while it is sill larger than 0, and on each iteration we add the remainder of a mod operation to the
empty binary array, then divide our integer by 2 for the next iteration. After we have hit 0, the binary
array will be holding all of our digits, but in the wrong order, and as integers. So we reverse
the array, then join it, which will automatically convert each array value to a string and concatenate them.
Running this through the test gives:
1 2 

Ok so this works, is easy to understand, and is relatively short. Let’s try to make it recursive. Whatever is in the while loop should be able to be pulled out and recursed upon. The base case would be when we have
an integer of 0 or 1, we can simply return that integer. Because we can combine the elements in the correct order as part of our recursive step, we don’t need an array, and will call to_s
on the base
case and the remainder to always return a correct binary string representation.
1 2 3 4 5 6 7 

Running this through the test also gives correct results:
1 2 

Let’s expand our testing a little bit by checking all numbers 0 through n
, and testing them out. How can we test for correctness without manually typing out each value’s string representation? Here
is where I show you the much easier builtin method for converting that already exists in ruby. Are you ready for it?
1


A ruby Integer
has the ability to change its base through the to_s
method. To convert an integer to binary, being base 2, you simply call .to_s(2)
to get the binary string representation.
Knowing this, we can write a test suite to check the correctness of our method much more exhaustively, with over 1 million checks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 

1 2 3 4 5 6 7 8 

Running this, we spot an error! Turns out the while loop method fails for 0. Let’s add a simple check for that edge case:
1 2 3 4 5 6 7 8 9 

Running the fix, we are all good now.
1 2 3 4 5 6 7 

So, the recursive method is about twice as fast as the iterative one, but how does they compare to the native method? Let’s add a third method convert_to_binary_native
and run the tests again.
1 2 3 

1 2 3 4 5 6 7 8 9 10 11 

Ruby’s implementation is faster by an order of magnitude. Their implementation of to_s
on a Fixnum
bubbles down a couple of lower level methods until it hits a core
C
function in the numeric.c source. It’s been awhile since I’ve worked in C
, but
it appears that there is a base case when the number is 0, then a do/while loop which the current digit is shifted and modded by the base. This method is essentially what we have in our convert_to_binary
method as defined above, but is obviously much faster as it is written in pure C
.