DEV Community

Cover image for Binary Numbers for Dummies
Roshan Ezra Jagdish Nayak
Roshan Ezra Jagdish Nayak

Posted on

Binary Numbers for Dummies

Picture this. You’re at the bank with your wife, waiting eagerly your turn to apply for a mortgage. You and your wife worked hard for months to rack up a good credit score, and it was all for this day. You’re in the midst of a conversation about the type of flooring the bathroom would have, when suddenly, the door explodes open! A couple of masked men start lighting up the place and yell for control. One of them grabs the nearest person as a hostage, which, unfortunately for you, is your beloved wife. Womp womp.
It eventually becomes apparent that they’re after the contents in the bank’s vault. You’re pleading for your wife’s release but to no avail. They don’t even bother to subdue you; you’re that non-threatening to them. They find the vault, and there’s a sticky note with the heading “Password” next to the keypad. Just one problem, the note is in 1’s and 0’s and the keypad has digits from 0-9. One of the robbers barks, “You! Nerd with the glasses! What’s the code?!?”. You stammer softly that you have no clue. The robber screams, “Solve it or she dies!”, cocking his pistol and pointing it to your wife’s head. You’re aghast. Bewildered even. You watch your wife’s eyes well up. “I’m sorry”, you say, your head down in shame. Your wife suddenly lashes out, “I knew I should have dated that MIT professor guy; he could’ve saved me today!”. You fall to your knees as tears drip from your face. He was a psychology professor, but that was irrelevant now. Only if you’d known how to translate all those 1’s and 0’s... Only if you had a chance to turn back time and prepare for this predicament...

Well, that is what I’m here for today. By the end of this article, you’ll know how to confidently navigate around those 1’s and 0’s – which are called binary numbers. You will also walk away with other useful knowledge, such as a useful binary finger technique.

What are binary numbers?

Binary numbers are simply another way of representing numbers. Just like the numbers that you’re used to in your daily life, i.e, decimal or base 10 numbers that use 0-9, binary numbers follow similar principles. Therefore, you must first understand how base 10 numbers work.
You’ve heard that going left to right, the digits in the decimal numbering system are referred to as “ones’, tens’, hundreds’, thousands’...”. This is virtually the same if we take 10ˣ, where x = 0,1,2,… For instance, 10⁰ is 1, 10¹ is 10, 10² is 100, and so on. Take for example the number 302 (three hundred and two). It’s written that way because 302 ≡ 3(10)²+0(10)¹+2(10)⁰. The decimal numbering system is thought to have emerged because of the convenience of having 10 fingers. It made it simple to count and keep track of numbers.
However, this is 2024. Not everyone has 10 fingers, we have to be inclusive even of 2 fingered people and aliens. Jokes aside, binary numbers are primarily how we communicate with technology. Much like the name implies, binary numbers use just 2 digits, i.e, 1’s and 0’s to represent numbers. The simple idea behind it is that a connection can either be closed (0), or open (1). It is the “language” through which electronics, like your phone, calculator, or console works. Just like how we used 10ˣ for base 10 numbers, we use 2ˣ for binary, or base 2 numbers. So, 2⁰ is 1, 2¹ is 2, 2² is 4, and so on. To represent the same number 302 in binary, it would be 100101110. How did we get to this number?

How to represent base 10 numbers in binary:

  • First, start with the biggest 2ˣ that doesn't exceed your number.
    • 0 - 2⁹ is 512, which exceeds 302. What about 2⁸?
    • 1 - 2⁸ = 256, which is good. We write a 1 and move on to the next step. We have to keep in mind that we add the various 2ˣ’s, just like in the base 10 example.
  • We then move to the right. Without skipping over anything, we have to go over 2⁷, 2⁶, 2⁵,..., 2⁰. We have to assign values of either 1 or 0 depending on whether we use the specific 2ˣ. So far, we have 256. Our next addition to this 256 shouldn’t exceed our initial goal 302.
    • 0 - 2⁷ is 128, and 256 + 128 = 384. No bueno.
    • 0 - 2⁶ is 64, and 256 + 64 = 320. No bueno.
    • 1 - 2⁵ is 32, and 256 + 32 = 288. Bueno! We will now use the 288 in place of 256.
    • 0 - 2⁴ is 16, and 288 + 16 = 304. Ouch, almost but no.
    • 1 - 2³ is 8, and 288 + 8 = 296. This is less than 302 so we take it as our next block.
    • 1 - 2² is 4, and 296 + 4 = 300. Bueno.
    • 1 - 2¹ is 2, and 300 + 2 = 302. Perfect.
    • 0 - 2⁰ is 1, but we already got our desired number 302. This will be a 0.
  • So in the end, we have 100101110, or 1(2⁸) + 0(2⁷) + 0(2⁶) + 1(2⁵) + 0(2⁴) + 1(2³) + 1(2²) + 1(2¹) + 0(2⁰).

Now, practice by converting the following into binary.

  • 519
  • 1023

Binary to decimals:

Now, how would you look at those 1’s and 0’s, and convert them back to base 10? This should be pretty simple reverse engineering. Let us consider the binary number 10011101.

  • Count up the number of digits. Let us call this y. Then, the starting point of your calculation is 2ʸ⁻¹. Here, there are 8 digits. Therefore, the very first digit is 2⁷, which is 128.
  • Move to the right and keep adding. Ignore 0’s, but reduce 1 from the power each time you do so.
    • 2⁶th place is 0, ignore.
    • 2⁵th place is 0, ignore.
    • 2⁴th place is 1, so add 128 + 16 = 144
    • 2³th place is 1, so add 144 + 8 = 152
    • 2²th place is 1, so add 152 + 4 = 156
    • 2¹th place is 0, ignore.
    • 2⁰th place is 1, so add 156 + 1 = 157
    • Therefore, 10011101 in base 2 is equal to 157 in base 10.

You are now equipped and qualified to help robbers crack the vault code! No longer will your wife regret choosing you! So, scenario #2. You’ve proudly and confidently helped the robbers crack into the vault. You grab your wife’s hand and turn to leave. But alas, this bit is still being dragged out. One of the robbers turns to you and says, "Alright, smartypants. If you're so clever, tell me what 746 is in binary. You've got 10 seconds!” How this robber suddenly knew about binary numbers is a serious plot hole, but let’s move on shall we?

The secret finger technique:

This technique will allow you to quickly calculate the binary representation of numbers up to 1000! It’s pretty simple, and the only prerequisite is having all your fingers intact.

  • Hold up both hands in front of you, palms facing away. Your fingers now represent the powers of 2, from right to left:
    • Right thumb: 2⁰ = 1
    • Right index finger: 2¹ = 2
    • Right middle finger: 2² = 4
    • Right ring finger: 2³ = 8
    • Right pinky finger: 2⁴ = 16
    • Left pinky finger: 2⁵ = 32
    • Left ring finger: 2⁶ = 64
    • Left middle finger: 2⁷ = 128
    • Left index finger: 2⁸ = 256
    • Left thumb: 2⁹ = 512
  • To represent a number, simply raise the fingers that correspond to the powers of 2 you need to add up to that number. For our example of 746:
    • Raise your left thumb (512)
    • Raise your left middle finger (128)
    • Raise your left ring finger (64)
    • Raise your left pinky (32)
    • Raise your right ring finger (8)
    • Raise your right index finger (2)
  • Now, read off the binary number from left to right. Raised fingers are 1, lowered fingers are 0. So, 746 in binary is... 1011101010!

Wrapping up

Binary isn't just for swooning your wife at a bank robbery. It's the fundamental language of computers -- the building blocks of our digital world. We've seen how those simple 1s and 0s can represent any number, and how we can convert them back and forth from the decimal system.

Thank you for reading. Please interact with the post. Until next time!
-- Roshan Ezra J. Nayak

Top comments (0)