K. Sabbak

Code Princess

00100001

March 09, 2018

I've slowly been making my way through Geek Sublime: The Beauty of Code, the Code of Beauty in my free time and one of the first thing the book tackles is how people work in the tech industry and don't know how computers work. As someone who entered tech with a bootcamp background, this rings true to me. Though we spent a good deal of time immersed in learning how to code, and managed to get some of the basics of web protocol, we certainly didn't touch on logic gates or binary. To be honest, I can't tell if Chandra believes that people who work in tech need to understand how computers work on a fundamental scale, but he does help demystify some of it.

I'm kind of lucky, due mostly to an interest in the subject and the vague idea that I might get a CS minor (I never did), I managed to take a few classes in college that set me up with some of this basic knowledge. I also held onto the notebook that was my trusty companion through these classes.

I mean how could I get rid of something this cute?

I decided to revisit the knowledge and see if I could share it here and I learned one very important thing: I took terrible notes in college. I know I learned how to convert decimal numbers (base 10, the number system we humans use day-to-day) to binary numbers (the 0s and 1s). I know I was tested on it. You'd think I'd've written down the formula. You would be wrong.

Luckily Chandra's book helped jog my memory. Here are the steps:

  1. Find the largest power of 2 (2n) that is less than or equal to the decimal number you're converting
  2. Write out that 2n in a box, going from left to right (like one normally writes) make boxes and fill them with the next smallest power of 2 until you get to 20 (aka "1")
  3. Below the box with the greatest 2n, put a 1, then take the decimal representation of 2n and subtract it from your original decimal number
  4. Take the difference of the step above repeat step 3 until you're left with 0.
  5. Fill in the rest of the boxes with 0s.
  6. You're done!

Here are the steps with pictures to help illustrate my point. The decimal number I chose is 713.

First we find the largest 2n that is still smaller (or equal to, but in this case smaller) than 713 - 29 or 512
Then we create our boxes and fill in the first one.
We take 201 (the difference of 713 and 512) and subtract 128 (27) from it, since 28 (256) is too large. And fill in the 128 (27) box.
We repeat this step with 73 (the difference of 201 and 128), this time subtracting 64 (26) from it and filling in that box.
We repeat again, this time with 9 and 23
And finally we repeat one last time with 1 and 20
Then we fill in the zeros on the boxes we skipped. This gives us 1011001001
And for fun, we check it against Google's calculator. It also gave us 1011001001. We did it!

You may notice that Google has "0b" in front of its answer. This is so we know that it's the binary representation of 713 and not the decimal number 1,011,001,001. This isn't the only way to denote something is a binary number but it is pretty common.

You may have noticed something was missing: Negative integers. In the interest of not turning this blog post into something way too long to handle, I'll leave you with the same cryptic nonsense past me wrote in my notes: "Also, 2's compliment[sic] is Awesome."

Tags: binary fundamentals old-school