hey let's talk about operators in C
this is another beginner C video. At this point you've probably seen a couple
of the different operators that C has to offer. If you haven't. If you are truly
brand new—you've never seen a line of C before and you're just starting from
scratch, you should go back and watch a few of my other beginner videos to get
you up to speed. They'll get you to where this video will make more sense. For the
rest of you, let's talk about operators. So what's an operator? An operator is
basically just the operations— mathematical or otherwise—that allow us
to modify or work on data in a programming language and C has a bunch
of different operators that you can use. So today I just want to look at them so
that you know what's available and you know how to use them. Okay, so, first on
the list are your arithmetic operators. These ones are pretty obvious. They're
your basic math operators—addition, subtraction, multiplication, division.
Super useful you've probably seen them. This probably comes as no surprise. You
might be less familiar with the modulus operator which [points to modulus symbol]. And, the modulus operator
gives you the remainder after a division. So, 4 mod(%) 2 equals zero (0) and 7 % 2 equals
1, and the modulus operator is super handy for things like telling
if a number is even or odd or checking to see if a number is evenly divisible
by another number. C also gives us increment and decrement operations. Now
these can be a little weird, and I'm going to explain why. So, if I have an int
with value 10 and I increment it like this, then its value is now going to
be 11. So, that's pretty straightforward but there are some weird things that you
might not suspect about the increment and decrement operators. Specifically
what if I write something like this? In this case, X is going to be incremented
but what is Y going to be? Y will actually receive the value that X used to be. When
I write x++, it increments the value of x, but evaluates to the old
value. Now, let's say I want to increment the value of X and read its new value.
Well, in that case I would put the increment operator before the X. And, the
decrement operator works exactly the same way. I know it's a little weird. It's
okay just play around with it. It'll make sense. So, next we have our comparison
operators. So, these are useful in comparing two expressions or two
variables or in representing logical expressions. So, the simplest of these is
the equivalence operator. Now for this one, pay attention. Because, in C and a lot
of other programming languages, one equal sign means assignment.
It means, I want one variable to get the value of another variable. But, if I put
two equal signs, that's testing to see if these two variables are equivalent. And,
of course, confusing these operators can cause some confusing and really strange
results in your programs. So, we can test to see if two variables or expressions
are equivalent. We can also test to see if they are not equivalent by putting an
exclamation point equals (!=), and we also have tests for greater than (>) and
less than (<) and greater than or equals (>= and
less than (<=) or equals. Now, each one of these
comparisons is going to evaluate to a value of true or false. And, we can
combine multiple comparisons—multiple trues and falses—to create logical
statements using the and (&&) operator, the or (||) operator and the not (!) operator. So, this
expression for example will be true if X is greater than Y and either a is
greater than B or a is greater than C. And, adding an exclamation point in front
will negate the statement, turning true to false and false to true. So, that's
straightforward enough. The next set of operators I want to look at are the
bitwise operators. So, for beginning programmers these may seem less familiar,
and to understand them we really need to look into how computers represent
numbers. An integer variable in C is a number and the computer stores it in
binary. My program may assign it to be 5 but internally it's stored as the binary
1 0 1. OK, so we call each 1 or 0 a bit and the bitwise operators operate on the
individual bits of a number. For example, if I take the bitwise and (&) of 9 and 5
it's going to go through each bit in each of those numbers, and if both bits
are ones it's going to produce a 1 and produce a 0 otherwise. So, the end result
is going to be 1. The bitwise or of 9 and 5 is going to be 1 1 0 1 or 13 and this
is doing the same thing as the and except it's checking to see if either of
the bits are ones rather than are both the bits one. Another bitwise operator is
the exclusive or or XOR and it produces a 1 if exactly one of the bits is one. So,
if you have 1 0 or 0 1 you're going to get a 1 but if both or none of the bits
are 1 it's going to be a 0. So, the exclusive-or of 9 and 5 is going to be 1
1 0 0 or 12. And, the bitwise not or complement operator all it does is it goes
through a number and turns ones the zeros and zeros to once so pretty simple/
And, then we have the shift operators. The shift right operator shifts the bits to
the right. The shift left operator shifts the bits to the left. So right shifting 9
by 2 bits is going to shift the number 2 bits to the right.
You're gonna lose those 2 bits, and the result is going to be 2. Another way to
think about the shift operators is that a right shift by 1 bit is like
dividing by 2, and a left shift by 1 bit is like multiplying by 2. And, that's
probably about as much talk about operators as I can handle for today. I'm
sure I'm missing a few, but I'm out of time. So, I hope that's a useful overview
for the beginning programmers out there. Who knows? Maybe even the more
experienced ones learn something. Either way, I hope it's useful. I hope it
helps you along your way to becoming a better programmer, and until next time
I'll see you later