Math. You need it. Just embrace it. Orrrr you could just stop right now and leave everything behind. I won't judge if you pick the latter.
In order to make anything interesting, you're going to need some math knowledge. These next few lessons are just a primer for some topics that you'll need to know to do things like detecting button presses.
Number Systems:
Background/Base 10:
The counting system we use everyday is called "base 10", and it's called so because we have 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (digits are numeric symbols that make up numbers). When we reach 9 (the last 1 digit number), we go to 10 and repeat the pattern we all know until we reach 99 (the last 2 digit number), then jump to 100, etc. Basically, when we run out of combinations, we tack on another digit. 9 is the last thing we can represent with 1 digit, so to represent more numbers, we add another digit and "reset" the rest to get 10. Now here's the thing: the representation of a number and the actual amount of things it represents are not strongly tied together. Just because we know that the quantity behind the number 10 is the same as the amount of fingers on our hands doesn't mean that 10 is the only way to show this. What if we had more than 10 digits to represent numbers? What if we had.... say, 16 digits, and they were 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F? We wouldn't have 10 fingers anymore, since we have a single digit to represent this many things. We would have A fingers. This is Base 16.
Base 16 (Hexadecimal):
Base 16 is a number system commonly used to express numbers in programming languages, and is called "hexadecimal". This means that, as we saw before, there are 16 possible digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. A is 10 in base 10, B is 11, C is 12, D is 13, E is 14, and F is 15. Once we run out of digits, we do the same thing we do in base 10: we tack on another digit and start over, so the next number after F is 10. You can see how this can get confusing: am I talking about 10 in base 10, or 10 in base 16? To make things less confusing, we'll use the SmileBASIC notation (symbol representation): hexadecimal numbers will have an &H in front of them. For example, we'll have &H7 for base 16, and just 7 for base 10. Keep in mind that because we have 16 digits, we can represent more values per position in a number.
So &h10 = 16. Huh, interesting. We can keep counting as usual, so &H11, &H12.... what happens when we get to &H19? Don't forget, we haven't run out of symbols yet, so the next value is &H1A. If we keep going up to &H1F, THEN the next number will be &H20, which is 32 in base 10. Hmm so &H10 = 16, and &H20 is 32. Can you guess what &H30 is in base 10? &H30 = 48. Don't worry if you didn't guess right! In base 16, each position of a number represents a power of 16 multiplied by the digit in that position. This is the same as base 10: the number 5,632 is 5 * 1000 + 6 * 100 + 3 * 10 + 2 * 1. 10^{3} = 1000, 10^{2} = 100, 10^{1} = 10, and 10^{0} = 1. In base 16, each position is a power of 16 because there are 16 symbols. So, &H10 is 16 because it's 1 * 16 + 0 * 1, and &H20 is 32 because it's 2 * 16 + 0 * 1. Using this knowledge, hopefully you can see why &H30 = 48: it's 3 * 16 + 0 = 48.
Let's try a three digit hex number using the above knowledge: What is &H123?
&H123 = 1*256 + 2*16 + 3*1 = 291
Just to show how this is similar to base 10, let's see how we would break down 123 (base 10):
123 = 1*100 + 2*10 + 3*1 = 123
Just remember: each position is a power of the base. In base 10, these are powers of 10: 1, 10, 100, 1000, etc. In base 16, these are 1, 16, 256, 4096, etc. This can make figuring out the "normal" base 10 value from a base 16 number complicated, but don't worry! As long as you know the concept, you can just take your time with a calculator and figure it out. You can also simply use a number converter, but try not to use these while you're learning, as the concept is very important.
Now try some on your own (without using an automatic converter):
&HF F = 15, so &HF = 15
&H2E &H2E = 2*16 + 14*1 = 46
&H3A &H3A = 3*16 + 10*1 = 58
&HFF &HFF = 15*16 + 15*1 = 255
&H100 &H100 = 1*256 + 0*16 + 0*1 = 256
&H3CD &H3CD = 3*256 + 12*16 + 13*1 = 973
&HFFF &HFFF = 15*256 + 15*16 + 15*1 = 4095
&H2000 *H2000 = 2*4096 + 0*256 + 0*16 + 0*1 = 8192
Don't worry if you couldn't do the examples. Take a look at the answers and how it got there, and try to see the pattern.
Hexadecimal outside of SmileBASIC:
You may know Hexadecimal if you've worked with colors on the web. They are usually expressed as 6 digit hexadecimal numbers such as #FF00FF. There are different ways to represent hexadecimal numbers; the most common is probably adding a 0x in front of the number, like so: 0x1A3. We will only use the &H notation here, as this is what SmileBASIC uses.
Base 2 (Binary):
For computers, it's hard to keep track of 10 different digits. It's MUCH easier to keep track of, say, 2 digits, and much faster for our current technology. So, we use a counting system called "base 2" (binary) which only uses 2 digits: 0 and 1. It doesn't matter how many digits there are, because we can still represent all the numbers. Think about base 16: we can already represent ALL the numbers with base 10, even though base 16 has more digits. It's the same with base 2: even though there's base 10 and base 16, we can still represent ALL the numbers we want with just combinations of just 0 and 1.
For binary, we will use the notation &B, which is what SmileBASIC uses. We start at &B0, then go to &B1. Crap, we ran out of digits; guess the only logical thing to do is to add another digit, so the next number is &B10. This is 2 in base 10. Now we can go to &B11, but oh jeez, we ran out of stuff again. Guess the only option is to jump to &B100. This is 4 in base 10. Let's look at a chart of the first binary numbers.
&B0 = 0
&B1 = 1
&B10 = 2
&B11 = 3
&B100 = 4
&B101 = 5
&B110 = 6
&B111 = 7
&B1000 = 8
&B1001 = 9
&B1010 = 10
...etc.
You may have noticed a pattern here: &B1 = 1, &B10 = 2, &B100 = 4, &B1000 = 8. You may be able to guess that &B10000 = 16, and &B100000 = 32. Just like each position in base 10 is a power of 10 and each position in base 16 is a power of 16, each position in a binary number represents a power of 2. Now we can see why &B111 = 7 : it's 1*4 + 1*2 + 1*1. Because you'll be using binary so much, it's probably a good idea to get familiar with the first dozen or so powers of 2:
1 = 2^0
2 = 2^1
4 = 2^2
8 = 2^3
16 = 2^4
32 = 2^5
64 = 2^6
128 = 2^7
256 = 2^8
512 = 2^9
1024 = 2^10
2048 = 2^11
4096 = 2^12
8192 = 2^13
16384 = 2^14
32768 = 2^15
65536 = 2^16
Now let's try some examples. Try to work through them on your own rather than using a converter. Note that, since binary can only ever be 1 or 0, I'm going to use shortened answers, as there's no point in putting the multiplication for 1 and 0.
&B1100 &B1100 = 8 + 4 + 0 + 0 = 12
&B1111 &B1111 = 8 + 4 + 2 + 1 = 15
&B10010 &B10010 = 16 + 0 + 0 + 2 + 0 = 18
&B11011 &B11011 = 16 + 8 + 0 + 2 + 1 = 27
&B10110101 &B10110101 = 128 + 0 + 32 + 16 + 0 + 4 + 0 + 1 = 181
It's important to note that in binary, a number with X digits can represent 2^{x} different values. This holds true for all bases: think about "regular" old base 10: a single base 10 digit can represent up to 10 values. 2 base 10 digits can represent 100 values (0 through 99). 3 base 10 digits can represent 1000 values (0 through 999). In binary, 2 digits can represent up to 4 values: &B0, &B1, &B10, and &B11, which is 0 through 3). 3 digits can represent up to 8 values (notice that the last 3 digit binary number in one of the above tables is 7, so 0-7 or 8 total values), 4 digits can represent up to 16 values, etc.
Why Hex is important for Binary:
It just is. No but seriously, hexadecimal and binary are related, and there's a reason why we use hexadecimal in programming so often. A single hex digit can represent 16 values, right? Weeellll 2^4 is also 16, and what did we just learn? That 4 binary digits can represent up to 16 values. So, any single hexadecimal digit can be represented by 4 binary digits, and vice-versa. Let's check out some examples!
Note that in binary (and sometimes in hex), we usually pad the front of a number with 0s so that it comes out to a certain number of digits. So, &B0001 is the same as &B1; we only do this for clarity.
&H1 = &B0001
&H7 = &B0111
&HF = &B1111
&HF0 = &B11110000
&H0F = &B00001111
&H59 = &B01011001
&HF0F = &B111100001111
Conclusion:
There's more than one way to represent numbers. The normal numbers that we use every day are "base 10" because they use 10 digits. There's also base 16 (hexadecimal) which has 16 available digits, which we represent with &H. An example is &HFF = 255. Computers store numbers in base 2 (binary), which has only 2 available digits (1 and 0). We use &B to represent binary numbers; an example is &B1100 = 12. Each position in a number in any base is the digit in that position multiplied by a power of the base. This is a complicated way of saying something you already know: the number 4783 is 4 * 1000 + 7 * 100 + 8 * 10 + 3 * 1. 1000, 100, 10, and 1 are all powers of 10, and the number is base 10. All other numbers in all other bases work the same for their respective bases.
2 Comment(s)xXOscarXxBeginner ProgrammerI'm just starting out! I'm still trying to grasp the basics.Programming StrengthDrawingI like to draw!HobbiesVideo GamesI like to play video games!Hobbiesh267RNGHiddenEaster EggsPokemon Is Awesome!I love Pokemon!Express YourselfGood PageHiddenAchievementsThanks! This will help me with my file compressor! ^w^