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 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:
- Find the largest power of 2 (2n) that is less than or equal to the decimal number you're converting
- 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")
- 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
- Take the difference of the step above repeat step 3 until you're left with 0.
- Fill in the rest of the boxes with 0s.
- You're done!
Here are the steps with pictures to help illustrate my point. The decimal number I chose is 713.
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."