LoginLogin

How To Program #7 - Number Systems

Root / Submissions / [.]

haloopdyCreated:
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. Next tutorial (part 8)

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?
answer&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. 103 = 1000, 102 = 100, 101 = 10, and 100 = 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
answer
F = 15, so &HF = 15
&H2E
answer
&H2E = 2*16 + 14*1 = 46
&H3A
answer
&H3A = 3*16 + 10*1 = 58
&HFF
answer
&HFF = 15*16 + 15*1 = 255
&H100
answer
&H100 = 1*256 + 0*16 + 0*1 = 256
&H3CD
answer
&H3CD = 3*256 + 12*16 + 13*1 = 973
&HFFF
answer
&HFFF = 15*256 + 15*16 + 15*1 = 4095
&H2000
answer
*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
answer
&B1100 = 8 + 4 + 0 + 0 = 12
&B1111
answer
&B1111 = 8 + 4 + 2 + 1 = 15
&B10010
answer
&B10010 = 16 + 0 + 0 + 2 + 0 = 18
&B11011
answer
&B11011 = 16 + 8 + 0 + 2 + 1 = 27
&B10110101
answer
&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 2x 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.
SpoilerNo 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. Next tutorial (part 8)

Thanks! This will help me with my file compressor! ^w^


This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.

Replying to:LucasJG1994
This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.
randomous is following the classic textbooks in introducing number systems here. A beginner could wrap their head around it with some effort, and the most helpful thing is familiarity--being able to recognize that "one of those weird bases is being used here" even if the real intuition doesn't come yet. It's not like it gets into floating point representation, it just introduces some concepts that will help with understanding bit masks (in collision and BUTTON) and that sometimes a given number may have a 'better' representation outside of decimal.

Replying to:LucasJG1994
This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.
@y_ack I do know that number systems are important. But these concepts are more for the intermediate level. Beginners don't need to be filled with this kind of complexity yet. All they need to know is how to write basic code, how to understand code structure, etc. Don't get me wrong learning about this is good. In fact a youtuber name Bisqwit used these concepts to make a Tetris game in GW-Basic. But still, I'd say it's not for beginners.

Replying to:LucasJG1994
This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.
I'm introducing number systems because in order to do even simple things in SmileBASIC, you should understand it. For instance, I could tell people "just do this to make buttons work" but then they're not learning. "Why does BUTTON() AND #A work?" "IDK, I don't know binary".

Replying to:LucasJG1994
This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.
I'm introducing number systems because in order to do even simple things in SmileBASIC, you should understand it. For instance, I could tell people "just do this to make buttons work" but then they're not learning. "Why does BUTTON() AND #A work?" "IDK, I don't know binary".
lol

Replying to:LucasJG1994
This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.
@randomous You don't need to know number systems to know how the BUTTON() keyword works. Even the manual doesn't go into number systems concerning that keyword. It just explains how to use it, what to expect when using the parameters, etc. Like I said it's ok to bring up number systems. But it's not for beginners. Intermediate learners would learn more from this since they would have a better foundation of the basics.

Replying to:LucasJG1994
This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.
@randomous You don't need to know number systems to know how the BUTTON() keyword works. Even the manual doesn't go into number systems concerning that keyword. It just explains how to use it, what to expect when using the parameters, etc. Like I said it's ok to bring up number systems. But it's not for beginners. Intermediate learners would learn more from this since they would have a better foundation of the basics.
Actually, one of the easiest ways to make a good game is by using dat files, or binary files to store data. Math is one of the most important things in programming. If you are a 7-year-old programmer and you haven't learned much math yet, you won't be able to do much. If you are a high school graduate, you are probably good at math and you will be able to do alot, because you know how to use trigonometry, algebra and all of that fun stuff. If you don't know how to use variables you won't be able to keep track of much things like items or scores very well. If you don't know about radians you won't be able to make a good bullet system for every angle (this happened to me, I don't know much trigonometry yet.) Therefore, you need numbers and math to make almost anything. I hope you understand my point now, and I hope you realize I am not trying to blast your face off or anything, I just want you to understand this.

Replying to:LucasJG1994
This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.
@random_god I can understand your point of view quite well. My point of view is more about the mind set of beginners when they have not coded anything in their life. All in all there is no 'right way' to teach programming. We all have our own ideologies and methods for teaching. I'm just the kind of guy that focuses on one step at a time and to get the basics grounded before moving on to other things.

Replying to:LucasJG1994
This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.
@LucasJG1994 I haven't read randomous's tutorials before because I already knew how to program before using smilebasic. Now that I read them, I support your point. However, I think this topic was included because those tutorials focus on smilebasic (And there are a lot of functions that use binary like button, sprites and map functions). Anyway, I think that this should be the last topic of all the tutorials. Also, this should include guidelines to use bitwise operations. PS: If all these tutorials are focused on newbies, then something that must be included is pseudocode. This is just my opinion. @random_god To my knowledge, dat file are useful to store assets (Graphic page, maps, wav, etc) since everything is load in an array(Example: BGLoad,GLOAD,etc). However, sometimes it's better to use text file instead since you're limited to use integer variables instead of char (You can use ASCII on dat files but it's easier to write text files), specially if you make your own way to load content. Some examples I've seen are Cave Island (The guy made text files to store map and scripting) and Jump Edit 4(Aka Mario maker clone where he store map and enemies using text files). Also, this tutorial only focus about number representation but not about what you can do with that.

Replying to:LucasJG1994
This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.
@raimondz Thank you for clearing things up. I have a more better picture now on this topic.

Replying to:LucasJG1994
This is definitely not for beginners. When you get into the different number systems and how they are used in programming. It can get extremely difficult. Quite a huge jump from the last post.
Actually, this helped my understand wavset in a snap.