Many of you have pondered in your spare time whether or not it is worth it to learn binary. Today, I’m here to tell you that if Flight of The Conchords can do it — you can too!

Let’s jump in

Here’s a simple case:

0000

This is an 8 bit representation of the number 0. Straight forward right? How do you think we would represent the number 1?

0001

Still with me? the last digit can only represent a 0 or 1. When it holds the 0 value, it passes on the value 0 to the computer. If it holds the value 1, it passes the value of 1 to the computer.

How do you suppose we do 2? Intuitively, we would just want to write a 2 where the 1 value in our previous example currently lives, but we can’t do this. At the core, computers can only understand 0′s and 1′s and thus the way that computers see a two is like this:

0010

In the case of 3 we just add what we’ve learned about 0,1,2 altogether.

0011

We keep the 2 from our previous example and add back the first position we talked about in the 0 and 1 case.

In the case of 4 we just remove all the 1′s we have and add a 1 to the next largest position:

0100

Now we can see a pattern, from 2 to 4 we had to remove the ones that were pre-existing for the odd numbers and move them to a ‘larger’ spot (the next one to the left).

As you’ve probably guessed, 8 is represented like this:

1000

Keep in mind that you’ll probably see 8 digits total when representing 8-bit numbers

0000 1000

With that representation we have a ‘byte’; 8 bits make up 1 byte. Therefore anything that is said to be ’32-bit’ is then 4 bytes. Interestingly enough, I’ve gotten this very question in an interview for a big company recently. Definitely good trivia to know if you want to be a programmer.

To represent binary numbers in your Chrome browsers:

1. Press command + shift + i
2. Then type in your number of interest (enclosed in parenthesis) then a toString function with the number 2 passed in the parenthesis.

i.e. (15).toString(2) should result in ’1111′ (aka 8+4+2+1)

Merry Coding,
Tony

Big news folks, I now live in SF and attend a programming bootcamp called Hack Reactor full-time; random, huh? I’ll get to the whirlwind of emotions in a follow-up blog post — but for now I want to talk about the topic of randomness as it pertains to programming and the world…kinda.

Today, I was confronted with a problem when I was told to write a function which produces a random integer in the range 1 to 7. For those who come from Javascript, don’t scoff at me as the solution doesn’t just iterate through Math.random functions.

As I was thinking about how to implement this the naive way, I realized that I would have to take into account uniform distribution and somewhat efficient memory usage. If you’re unfamiliar with uniform distribution, then think back to when you last had a final exam. Do you recollect how well you did in the aforementioned final exam? It was most probable that you fell into the middle of what some in the world of probability would call a normal bell curve or normal distribution. This is kind of the same idea except in this particular problem, we’re looking for something more random like a uniform distribution. Therefore the probability to obtain any number between 1 – 7 should be approximately .143 or (1/7).

With this goal in mind, we then have a few options. Let’s first start with the naive way:

http://gist.github.com/5230873

We know that the probability of choosing a random number between 1 – 7 is in fact 1/7 and therefore we’d miss a few range of numbers by not incrementing the 1/5 randomness given by the first random1to5 function. The function should probably take into account the 1/5th and augment the probability to match a ratio of 1/7 — 5/35.

http://gist.github.com/5230906

Here I chose to incorporate rejection sampling to filter out the left skew that I was running into earlier. Run these two in the browser to check out the differences!

I’ll be posting more about puzzles and my code on a consistent basis. Please feel free to post links to your solutions or any feedback you may have for me.

Cheers!

Anthony