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:
This is an 8 bit representation of the number 0. Straight forward right? How do you think we would represent the number 1?
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:
In the case of 3 we just add what we’ve learned about 0,1,2 altogether.
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:
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:
Keep in mind that you’ll probably see 8 digits total when representing 8-bit numbers
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)
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.
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:
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.
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.