Programming: Binary

    Programming: Binary

      Since the compupocalypse, it's been hard to get a decent cup of coffee. Everybody's jobs have been displaced, so they have nothing better to do than order seven dollar coffee at Diamondbucks. Today, though, you face the crowd head-on because you know their Thanksgiving special, the turkey gravy latte is finally in-season.

      Mmm, turkey gravy.

      You finally make it to the front of the line where a computer barista (compurista) waits to take your order. After writing your name on the cup, it tells you your drink costs $111.

      What? Starbucks is expensive, but geez.

      Just then, you realize it's telling you the price in binary. You ask it to convert that to decimal—the normal number system—but it tells you:

      "I'm sorry, I'm afraid I can't do that, Dave."

      They always get your name wrong.

      This situation might still be in the future, but computers don't actually know decimal. They work in binary.

      Modern day computers are digital, which means that they work based on pieces of memory being "on" or "off". The "on" state is assigned the value "1" and the "off" state is assigned the value "0". Each binary digit—0 or 1—is a bit and 8 bits make up one byte. So next time you hear people talking eight bit games, you can smugly correct them about how they're actually playing one byte games.

      How do you keep yourself from paying $111 when you only owe $7? You learn how to convert to binary, of course.

      Converting from Decimal to Binary

      In base ten, we represent numbers with ones, tens, hundreds, thousands, and powers of ten in general. For example, 1,527 is the same as:

      1 thousand
      5 hundreds
      2 tens
      7 ones

      If we look at this another way, we could also say that 1,527 is the same as:

      1 * 103, which equals 1,000
      5 * 102, which equals 500
      2 * 101, which equals 20
      7 * 100, which equals 7

      Or, if you like looking at tables:

      Thousands(103)Hundreds (102)Tens (101)Ones (100)
      1527

      There's one group of a thousand, five groups of one hundred, two groups of ten, and 7 groups of one.

      Keep in mind: any number to the 0 power is 1.

      Now let's take a look at a couple of numbers and convert them to base two, instead of base ten.

      (Coincidentally, base two just happens to be binary.)

      Method One: Table

      The number seven looks like it could use a makeover, so let's turn it into base two.

      We can start by subtracting the largest power of two from seven, which is four (22).

      1 * 22 = 4

      7 – 4 = 3

      Now we have a remainder of three, so we need to find the largest power of two to subtract from it. That's two (21).

      1*21 = 2

      3 – 2 = 1

      We still have one number left, though, so we find the largest power of two to fit in one, which is one (20).

      1 * 20 = 1

      1 – 1 = 0

      Altogether, we have:

      1 * 22 = 4
      1 * 21 = 2
      1 * 20 = 1

      4 + 2 + 1 = 7

      Cool, but we're still in decimal. We now translate that into Binary using our handy-dandy table:

      222120
      111

      If we skipped any powers of two as we subtracted from seven, then those spaces on the chart would've been marked with zeros. As it stands, the binary conversion to seven is:

      111

      Method Two: Division By Two

      Let's use the number 68 as our example.

      You can break down what to do into a handy-dandy algorithm:

      1. Divide the number by 2.
      2. Record the remainder. 
      3. Repeat until the number's zero.

      So 68 looks like this:

      1. 68 ÷ 2 = 34, remainder 0
      2. 34 ÷ 2 = 17, remainder 0
      3. 17 ÷ 2 = 8, remainder 1
      4. 8 ÷ 2 = 4, remainder 0
      5. 4 ÷ 2 = 2, remainder 0
      6. 2 ÷ 2 = 1, remainder 0
      7. 1 ÷ 2 = 0, remainder 1

      Every time we divide by two, we're essentially pulling off another power of two, so by the time we reach the bottom of the list (1 ÷ 2), we're actually dividing 68 by 26 (taking into account the fact that base two starts with 20).

      Once the quotient (a.k.a. the number on the right of the equals sign) is zero, write out the remainders from the bottom to the top in order from left to right. So the equivalent of 68 is 1000100. Let's check to see if we got it right.

      1 * 26 = 64
      0 * 25 = 0
      0 * 24 = 0
      0 * 23 = 0
      1 * 22 = 4
      0 * 21 = 0
      0 * 20 = 0

      Total it up, and you get 68. Sure is nice when the numbers all add up.

      Either method of converting to binary will work; it all depends on which one you prefer.

      Converting from Binary to Decimal

      How about converting numbers from binary to decimal? The process is the same as when we checked our conversions. For example, if we had the number 1001010, we would need to list out its values in the following way:

      1 * 26 = 64
      0 * 25 = 0
      0 * 24 = 0
      1 * 23 = 8
      0 * 22 = 0
      1 * 21 = 2
      0 * 20 = 0

      The result is 74.