Friday, May 5, 2017

Learning Binary

Binary 100 Simplified

Binary is the essence of all of existence. Everything can be divided down to the simplest form of computing, which is by using binary. Black and white, yes and no, true or false, good or evil are all examples of binary in action. But what about in computers? In computers, all programs are written using some other machine language that ultimately relies on some form of binary input. That is to say, binary is the primary building block of all other programming languages. Another language, which we can study later, is hexadecimal.
Just like binary, hexadecimal is not actually a language, it is a code of numbers. In the case of binary, the code is either zero or one. That is why it is called binary, because the only options are one of two.
So, in order to build binary code, we are limited to either using a zero or a one. To build a yes or no value is as simple as assigning one to yes and zero to no. In math, that looks like this: y=1 & n=0. In binary, it's even simpler: 0 or 1.
So you must be wondering, what about 2? The olde 1 plus 1. Well, a bit can only contain a single one value. So either a bit is empty, or a bit has a value of 1. So now, we can assign a set of bits either nothing or a 1. Zero is nothing, right? That's simple math.
1 is not-zero or true.
Zero is false, the bit does not have a value. It is false because there is no value assigned. You assign a value by not assigning the true value. That is the essence of binary: true or false.
In math and coding, zero is followed by one, which is followed by 10. In decimal (latin for ten), after nine, you start over back to a one after a zero. Same thing with binary, after one, you start over. In base-3, you start over after 2; so: 1, 2, 10, 11, 12, 20, 21, 22, 100, and start over again.
Same thing with binary, only there's only the one by itself. So in essence, we already are familiar with the concept, since we practice it in base-10 everyday by exchanging money in decimal-based denominations.
After 1 is 10, because there is no 2 in binary, only zero and one. After 10, 11, but then no 20, it's straight to 100, and that's our 4.
Interestingly, every power of two is the number of zeroes behind the one. So two squared, or 4, is hundred, eight is two cubed, which are equal to 1000 in binary format. Each bit is combined with seven other bits to form one byte of data. A byte is eight bits. So that was pretty simple. How did things get so complex?

Candy Crush

Binary is simple, there is a one and there's a zero. After that it's just more ones and zeros. So how does that apply to a game? Simple, the game was programmed with only 1's and 0's.
Each tile is connected on one side.
If the colors match, assign a 1.
If the colors do not match, then not 1, or zero.
So if a tile has the color blue and a blue tile above, then the game was programmed to assign a value of 1 to the space between them. Now if a blue is nearby, then a zero can become a one by switching the two tiles creating a zero value so that the further tile generates a second one value.
Thus, when two 1's are together, we see visually three tiles connected, but really, the programmer just created a simple equation that if 1 + 1 = 2, you can continue; if not, then it subtracts 1, and you see one less move available in the game.
Any questions? Basically,just ignore the hypnotic colors.

Applying the rule

The rule is simple, when two colors match, you have a value of 2, right?
Impossible. In binary, there is no two. There is only 1's and 0's. So how did they program a value of 2?
Well, in order to understand how binary can be used to program a game, you must first be able to see the number values associated with the binary values.

Binary Conversions

0 = 0
1 = 1
2 = 10
3 = 11
4 = 100
5 = 101
6 = 110
7 = 111
8 = 1000
Okay, that should be enough for us to figure out the coding behind a program such as Candy Crush or Soda Crush. It's not the colored tiles that you pay attention to, it's the value between each pair of tiles.