Basics of Programming #12 - All Operators

Dec 30, 2019 16:11 · 4818 words · 23 minute read parse given int

Oh, this will be fun – all of the operators in C#. Hello, everyone, and welcome to the last video until the, basically big separator between the intermediate phase and the absolute beginner’s phase. So, to round off this entire section, I decided we’d go through all of the operators together, as well as any concepts that go with them, like shifting left and right, for example. And, then, at the end, I’ll talk about where this series will go from here. Right! Here’s a list of all of the operators in C#.

00:46 - Yeah, there’s quite a few, but I think we can actually get through them quite quickly. You don’t need to copy anything down for this video, just watch it with your full attention because there’s quite a bit of theory involved in a lot of these. Right! Let’s kick it off instantly with the first five, which we already know. So, if I have an integer called “first” and an integer called “second”, then I can add them, so this will take first and add second to it, then use the “=” operator to put that into “answer”. We also have subtract, multiply, divide and mod, which would divide “first” by “second” and give us the remainder, very useful.

01:36 - Now, some different data types behave differently with these operators. So, if I add two strings like this then this will concatenate them, which is the technical term for joining them. Great! We blazed through those pretty easily, but we already knew those really well. Next we have these. Now, imagine “x” here is your variable. Now, there is actually a difference between having the “++” before and having the “++” after, which is why I’ve them both as separate, so, I’ll explain that. So, what these do is they add one to a variable.

02:16 - So, if I have a number that is 23 in a variable called “num”. Then, I can do “num++” and this will add one. So, if I debug and look at this variable – it’s 24 now! It went up by one. You should actually know this operator from our “for” loops, where we use it at the end here to increment “I” by 1. Now, if we use “—“, then surprisingly that will subtract 1 one from the variable – who would have guessed! Right, fine, but now, this is where we need to be careful.

02:53 - When you put the “++” before the variable like this, that will get the variable after it adds one to it. OK, let me explain what that actually means. So, I’ll make a two variables here, pretty simple, right? And I’ve just got a breakpoint on my ReadLine here. So, I’ll set this to 17. Now, if I set the second variable to our first variable++, like this – then what will happen? Well, let’s look at it. OK! So, you can see that our second variable is 17.

03:31 - But, then, if I look at the first variable – it’s 18! So, it has gone up, but it went up after we set the second variable, you see? However, if I was to write it before the variable, then it will go up before we set the second variable – so, that way the second variable will include that increment (increment is how you say go up by one, and decrement is how you say go down by one). So, that’s the difference – if we’re setting the value of the variable, it matters which way around they go – if the ++ is before, then it will add one to the variable and then use it for whatever it wants to use it for. Whereas, if we put it after then it will use its value and then after the rest of the line has run, come back and add one to it. In fact, you might have heard of the programming language “C++”. Which is basically an extension to the programming language “C”.

04:37 - Well, now you know that “++” means go up by one – so, it’s the next version up. The name C# is also following this rule, because that hash there looks like it’s made up of 4 pluses, so, as if we’re doing “++” twice, so, it’s two versions ahead of C, that’s the idea. Great! Next are the binary operators! Now, this is where we really start needing some diagrams and stuff in order to visualize what is going on. So, all of these take in two objects, which are usually numbers, but they can really be any data type, all of these depend on whether that specific data type supports that operator. Right! So, the first one is an AND operator, and in order to AND two numbers, we just do the first number and the second number, like this.

05:31 - But, what exactly does this do? Well let’s lay these two out above each other and show their binary format, in order to try and explain it. Now and will give us a back a number, right? So, we’ll also have a result, which will start off as 0, but we’ll fill this up as we go along. So, here’s what happens: We look at the first two bits and if they are BOTH turned on, then we turn on that bit in our answer – that’s it! So, in this case, they are definitely not both turned on – in fact, neither of them are on. So, that’s a “0”. Next, we have one of them turned on… But the other still isn’t turned on so we keep that as “0”. However, the next one has both of them turned on, so we put down a 1.

06:22 - And, then, we just go through all of the rest of the bits and that’s it. That is AND! Next, we have the OR operator, so, this will say if either the first one is on, OR the second one is on. So, in this scenario, neither of them is on. So, this stays turned off. But, in the next bit at least one of them is on, so we turn the output on. And, in the bit after that the same still applies, at least one of them is on – in fact, both of them are on, so that will become a “1” as well.

06:59 - Now, this line here isn’t an “I”, it’s a pipe character, you’ll probably find it next to the left shift key on a British keyboard – just look up “where is the pipe character on…” followed by your keyboard type to find out how to type it out. OK, next is XOR, which stands for Exclusive OR. And, an Exclusive OR is particularly important in a CPU – the ALU (the thing that does the maths) actually uses tons and tons of XOR gates all joined together – that’s how computers add and subtract binary, they can actually use lots of XORs joined together. By the way, I just want to say that you will almost never use these operators in your code – in fact, I can say that I almost never have, and I’ve been doing this for years… You just… Don’t need to really. But I am telling you about these just so that way if you see them in someone else’s code, you don’t have to worry because you know what they mean.

08:02 - So, XOR is basically a mixture of an AND plus an OR gate. So, XOR will only give a “1” if only one bit is turned on. So, for the first one, they are both off, so that’s off. For the second one, only one of them are on, so that’s “1”. However, on the next one, both of them are on – so that’s a “0”. Only when one of them are turned on is this bit turned on. In case you’re curious why adders use these, I’ll very quickly explain, but I’m not going to go in-depth on that, because, well, it’s really not relevant, but if you want to me to make some separate video about that then I can do that. So, first, in order to add two binary numbers here’s what happens – now, we’re starting from the right, because that’s how you add binary numbers. So, per each bit, this is what we do. If we’re adding a “0” and a “0”, then that’s “0”. If we’re adding a “1” and a “0”, then that’s a “1”, and if we’re adding a “1” and a “1”, then we turn on the next bit down, and this bit will stay as 0.

09:12 - Because, think about it, in this case 1 plus 1 is two – so it turns on the next bit down, which is two. Now, there’s also the final scenario, that if you have 3 “1”s, so if we carried over a “1” from the last bit as well like this, then you make this one a “1” and the next one a “1” as well, that’s how you add binary numbers! So, in the ALU, it uses an XOR gate, in order to work out what this bit would be. So, this is how the CPU would do this. If we have a “0” and a “0”, then the XOR gate would give you a “0”. If at least one of them is on, then we would get a “1”, right? And, finally, if both of them are on, then we would get a “0” on just this bit here. So, now, you just have that for every single bit (so most CPUs are 64-bit, so that’ll be 64 XOR gates), and you’ve got half of an adder.

10:11 - Then, the other half of the adder would be in charge of making the next bit a “1” if both of these are turned on and would be in charge of handling three ones – and that would be all you need and you can now add binary numbers! There’s actually a video of someone making an adder with dominos, and it works exactly like I just explained – once they had worked out how to make an XOR gate and an AND gate with dominos, they were able to make an adder. So, yeah, that’s a little bonus I guess, that’s how CPUs add. And subtracting is mostly the same, but I won’t get into that. OK! Back to where we were, next we have shifting left and shifting right. So, these two are probably the binary operators you are most likely to see, if any at all, I have used these quite a few times – but that was mainly to do some pretty complicated data transfer between C++ and C#.

11:06 - So, let’s say we have an 8 bit number here (once again, integers in C# are 32-bits unless you specifically say how big you want them, but just to keep this simple we’ll imagine they were 8-bits). Now, when we shift left we are pushing all of the bits to the left by that amount. So, let’s say we have this number (170) Now, if I shift to the left by three, so, if I write this in the code, it will push all of the bits over to the left by three – like this. Then, it will take all of the bits that have fallen out of the size of our number and throw them away, and it will fill in the spaces we just created with “0”s. And, shifting right is, well, the opposite, so you push the numbers to the right.

11:53 - Actually, you’ll find that a lot of the times you use shifting is to actually throw away some bits, so, if I for some reason decided that I wanted to get rid of these first 4 bits I could just shift left by four, then they get thrown away like this. And back to the right again by 4 to put the other bits back in place – and, look, those first four bits have now been thrown away and replaced with zeros. But, really, it depends on how big your number is… Because what I actually could also just do here, and what I would probably just do here is to AND this number up against “15” like this. And that would do exactly the same thing – it would leave us with only the last 4 bits as well. Because, if I look at it like this, the first four bits will always be turned off, right? Regardless of whether these four are turned on or not, the first “4” bits will always just be “0”, because they’re 0 in the second number.

12:55 - However, the last 4 bits can be turned on. So, I suppose that is one of the most common uses of AND – it’s to limit a binary number to just a selected amount of bits. But, I think at this stage you probably won’t really use it. OK! Now, unlike all of the other binary operators, this one only takes in one number. This is the invert operator – so, this will invert all of the bits in a binary numbers, it’s extremely simple to explain.

13:25 - If a bit is a “0”, it becomes a “1”, and if it’s a “1”, it becomes a “0”. So, just do that for all of the bits and that’s… it. To use this invert operator just write the tilde sign followed by the number to invert, not the dash – I’ve seen so many people do this – it’s this key right here on a British keyboard. And it’s this key on an American keyboard – not a dash. OK! So, that’s all of the binary operators, hopefully all of that makes sense, ignore the adder stuff, you don’t need to remember that, just remember AND, OR, XOR, INVERT and shifting left and right.

14:07 - Now, let’s move onto the Boolean comparison operators. So, these operators will compare two things – and, they can be pretty much any data type – almost all of the data types support these. And, these will generate a Boolean based on a condition… I pretty much already covered these in the last video. So, here we go… Now, if we look at the value of these Booleans by debugging. You’ll notice that this is true – they are equal.

14:36 - This is false, they are equal, and, this is checking if they are not equal, but they are, so, this creates “false”. Next, we have a greater than sign, in this case… This is obviously bigger than this, so that’s true. And, here, we have a less than sign and this isn’t smaller than this, so that’s false. Right, now, those are operators that make Booleans, but these operators now take two Booleans and do things with them. Now, we’ll find that all of these are very similar to three of the binary operators.

15:14 - The exclamation mark is similar to the invert operator. The two AND signs is very similar to the binary AND sign, and the two OR signs is very similar to the binary OR sign. So, the first one is the NOT operator. And, it will just invert the Boolean. So, if the Boolean is false – it becomes true, and if it’s true, it becomes false. This operator, just like the invert operator only takes in one thing – let’s look at it. So, we have two Booleans here, “bl1” is false, and “bl2” is set to the NOT of “bl1”, so it will invert what “bl1” is.

15:58 - Now, here’s my question: What will “bl2” be set to? Well, the answer is true. The inverse of “false” is “true”, so “bl2” is set to true. Now that we know that, why one of the previous operators is called exclamation mark equals should make sense. It’s literally saying NOT, because an exclamation mark means NOT, then it says equal to. Now, remember how I said that we don’t have to do this – so, we don’t have to do “== true”. Well, we don’t have to do “== false” either. If I want to check if a Boolean is true… I can just do exclamation mark, followed by the Boolean. That will check if the Boolean is false. And, of course, if you think about it – it makes sense. If the Boolean was false, it would get turned into true, so the “if” statement runs, and, otherwise, if it’s true, then it becomes false, so the code in the “if” statement will only run when it’s “false”. So, you’ll see me writing stuff like this a lot throughout this series when I’m working with Booleans.

17:17 - This means if it’s true, and this means if it’s false. Right! Now, we’re on the final two operators. The Boolean AND operator will take in two Booleans and give us a Boolean back telling us whether the same are both true. So, very similar to the AND operator, if both of the Booleans it takes are true, this will give us true, otherwise, it will give us false. So, here we are – another question, will this give us true or false? Well, it will give us false, because these two are not both true, are they? But, what if… I did this? Now what? Well, the answer is actually true.

18:02 - But why? Well, just think about it logically. So the stuff in the brackets runs first. So, this AND operator checks if both of them are true, which they aren’t… So this gives us false… BUT THEN we have our NOT operator, and this will invert that “false” into a “true”. Now, the brackets are important here… Because, if I wrote it like this… This would be quite different. The “!” operator would run first. So, it would invert this true… Into false. So, now, I have false here and false over here – so that’s definitely not going to give us true, they are not both true, are they? However, if I moved the exclamation mark over here, now this will give us true… Because the “false” will be inverted from a false into a true, so, then, we have “true” and “true”, don’t we? So, yeah, I will just keep repeating that brackets are important not just for numbers, but for handling the order of how things will go. Now, the final operator is an OR.

19:21 - And this be true if at least one of the two Booleans are true. So, these are the values I would get. If the first Boolean is false… And the second Boolean is false, then it would give us false. However, you’ll notice that for all of the rest the Boolean OR operator will give us true. Because at least one of them are true, aren’t they? Now, you might be wondering: “What happened to XOR?” And, well, it turns out that in C# you just use the normal binary XOR operator if you want to XOR two Booleans – I didn’t actually know that because in all my years programming, I have never had to XOR two Booleans, I’ve had to “AND” and “OR” two Booleans, definitely… But I have never used an exclusive OR on two Booleans before. Anyway, this is called a truth table right here actually.

20:12 - A truth table is exactly what I’m showing you right now, it’s a table that has a list of all of the possible inputs, and then the output you would get for each of them – here’s a truth table for the Boolean AND operator as well. Now, we can’t really use the truth table for the binary operators because those inputs could just go on infinitely, couldn’t they? The number could be 30 bits long, could be 40 bits long, it’s impossible to make a truth table out of those. But, this is a truth table of what happens per bit on those binary operators! OK, just two more things to mention. Firstly, all of these operators do not change the original variable – so, for example, if I shift a variable by right by 3… It will not change that change, it will just give me that number shifted right – just like with “ToLower”, these don’t change the variables themselves – they give me the modified value back. So, in order to fix this, I would have to set them like this – so, if I’m adding “3” to the variable “j”, and I want to set “j” to that, and I just do “j =”, followed by the “j + 3”.

21:26 - But there is a shorthand way of doing this, instead of writing this, I can write “+=” 3. Instead of writing this, I can write “-=”. Instead of writing this, I can just use “*=”, this is a short way of doing it. I can even do that for shifting left and right if I want! Alright, the final thing I want to talk about then, is that, these operators do have an order of execution. Notice how I said that an “!” operator runs before the Boolean AND operator would earlier? Well, if you leave it with no brackets, different operators are considered more and less important than others.

22:12 - So, the more important ones will run first, then the less important ones will run last. Now, I personally don’t know the operator importance - in fact, there is absolutely no point in remembering them, really, I just put brackets, it’s just clearer what will actually happen like that anyway. But, if you are a little bit curious this a table from the official Microsoft website, and, Microsoft is the company that owns C#, so, this is directly from the people who actually make C#. This table tells us the importance of operators. So, at the top we have the most important, and at the bottom we have the least important.

22:53 - Now, let’s just ignore this first row, this entire row is basically entirely stuff that we haven’t learnt yet. Now, if you look, a “prefix”, which is where you have “++” or “—“ before the variable is of course the highest priority, which is basically the one thing that you should know from this list, before that’s what I said when I explained the operator. A prefix will run first, before anything runs, won’t it? So, feel free to look through this on your own, it’s interesting to see which operators will have a higher priority. Right! Now, I’m going to talk about where we’re going with this series. So, so far we’ve covered the absolute basics, so, they’re the things that you can’t go without – but, that does not mean that we are done – not, definitely not, there is so much more to cover.

23:45 - For example, if you wanted to store 100 names – how would do that? Well, at this stage, you’d have no idea how to do that, but it’s done using something called arrays. Later on we’ll start making methods, which is the ultimate code, then, we’ll talk about classes, which is where you can make your own data types – and that’s where things really start getting interesting… Then, we’ll go even further on from that and start covering namespaces, inheritance, generics, joining multiple projects together – by the end of the series, you will know the entire language. And, by the end of this series, you’ll definitely understand what all of this means… You know how when you make a project it leaves you with all of this stuff? Well, by the end of the series, you’ll know what all of this means. Here we’re making a namespace… we’ll cover that, here we’re making a class… we’ll cover that, then we’ve got a method here, and, if you look here – we actually have a “string array” as well. This is making an array of strings… These are all things we will cover, and by the end of this series… You’ll know exactly how all of this stuff works! So, what do I want you to do? Well, I’ve decided not the set you any tasks since, well, we haven’t really learnt anything since the last video – but, what I do really want you do when you go away from this video is to take a break from the series, and start your own projects.

25:23 - Start making your own projects, they could be games… They could be projects that help organize your files (although that might be a bit difficult without arrays), whatever it might be – make something that you can do at this level. Right, just an extra little thing from me that I think will be really interesting, maybe not right now, but later on in the series… There’s a website, once again on the official Microsoft website called “ReferenceSource”. This lets you look at the C# code of a lot of the things we’ve run. So, for example I can look at the code of something like “Console.WriteLine” or “int.Parse”. Now, “int.Parse” is actually under “Int32.Parse”, because “int” is technically a short way of writing a 32-bit integer, so, yeah, I’ll just search “Int32.

Parse” – and look, 26:15 - there it is, I can see it says in brackets “System.String”, that’s telling me it takes a string, and I can just click on it and look at its code. Now, at this point, almost none of this code will make sense, ok? So, as I scroll through this, I expect you to understand very little of what I’m doing. But, what I think would be nice is that every few episodes, we come back here, and see if you understand a bit more – keep in mind, we’re looking at the code for a lot of the parts of the language itself – of course it’s not going to be easy, right now I’m just demonstrating to you that this is a thing. So, it’s a bit odd to go around, so, I can see there’s nothing really, in here, but, if I click on “ParseInt32” here, then it takes me down further into the rabbit hole, so, now we’re getting deeper and deeper.

27:04 - Now, this is thing has an “if” statement that is checking whether our number is a hexadecimal number or not. Now, all of the numbers we’ve given “int.Parse” have NOT been hexadecimal numbers, so, let’s ignore all of this stuff inside this “if” statement, and look inside the “else” here, and we’ve got this “NumberToInt32”… OK, that sounds like something we might want, so, we’ll click on that… And now this is the actual bit of the code that does stuff – this is the bit that actually turns that string into a number. Now, this looks really complicated (because it is really complicated), and this is where you just have to pick out the bigger picture – ignore all of the smaller details, just look at the bigger picture. So, we have a while loop here… And look, inside this while loop we can see that we’re decrementing, moving down this “I” variable down by one. And we’re going to continue to do that until “I” has gone below “0”, because, it will only run this “for” loop while I is 0 or above.

28:08 - So, what this is doing is it’s going backwards through our string – because that’s how you turn text into a number, you have to start from the end and work your way back. And, then we’ve just got a bunch of maths and all sorts, this video is getting quite long, so I won’t get into it, but, when you really break it down, it’s not that hard. Now, yes, there are TONS of things all over the place here that we haven’t learnt yet. But these are all things that we will learn! And by the end of this series, you will be able to understand how every single line of this works, it’s going to be a lot of fun. So, thank you all so much for watching this video… In the next video, I’ll talk about arrays, which are extremely important, once you’ve learnt them, you’ll wonder how you survived without them.

28:56 - OK, have a go at your own projects, and I’ll see you all next time… Bye!.