Hey guys.
So we're now going to discuss what pit operations are in C but to really understand that we first need
to understand what pit operations are.
So essentially a process has a bunch of instructions to perform operations at the BET level.
And there's a bit wise and a bit wise or a bit wise XOR and so on.
But let's now explain this.
We're gonna start with bit wise and so I want you to select the binary here.
This is a calculator at easy online converter dot com.
It's the bit Y's calculator.
You don't have to do this just follow what I'm doing.
Follow the video so you understand.
So number one I'm going to enter eight bits and they're all gonna be ones one two three four one two
three four.
And then number two I'm going to do the same one two three four one two three four.
And I'm going to make sure the bit wise operator and is selected I'm gonna press calculate so we can
see the results is eight bits and that all equal to one.
So what happens is the process checks the first number you entered the first bit and then it compares
it to the next but on the second number.
If they are both one the the output for that bit is 1 and then it goes to the second bit is this one.
Yes.
Is this one.
Yes.
OK.
So that bit is one.
And this is how it calculates your result.
If we put a zero here and press calculate we see 1 1 1 1 1 1 1 0 1.
And the reason for that is because this bit is 1 this bit is 0.
So it doesn't set off satisfied and the end operator and therefore this bit in the result is zero.
So that is how end works.
So essentially if these are all zeros one two three four one two three four.
And we press calculate the result is zero because it goes to each individual a bit.
It checks this one against this one.
And the bit in the result is only 1.
If both the bets in number one and number two also 1 and that's binary but you know decimal works the
same year we can put 2 5 5 here and 2 5 4 here.
Press calculate and we get to 5 4 250.
Calculate two fifty to ten and then we get to 10.
So it works the same because remember everything is just binary and decimals is just a way of expressing
that binary.
So we're now going to do this in the code.
So I want you to go unsigned char a equals zero B for binary one two three four one two three four unsigned
shot B equals zero B one two three four one two three four.
And then we're gonna go unsigned char result equals a ampersand B.
Notice how I only use one Ampersand here.
This does a bit wise and on these two numbers if we now compile that
GCSE main Delta C Dash I main main and we see nothing because I haven't I would put it in.
But if we do print f I for a session are results and we compile that.
Now we now see 2 5 5 because the process checks this one it says yes this is one this is one two.
So this bit in the result is one this bit is one this bit is one 2 so that it in the result is 1.
So we end up with decimal two hundred fifty five which is this is the binary equivalent of decimal two
hundred fifty five.
And bit wise and is great for checking if a flag of some kind is set for example if I go up here and
I do define I don't know special flag and I do zero B one two three four one two three four and I get
rid of the final zero and put a one and if I now do and I get rid of this if I now do if a and special
flag what will happen is if a variable if the final one here if a variable also has a final one then
this if statement will we run flag set.
So if we now compile that we can see flags set.
However if we don't have that flag we put a zero here we see nothing.
So by using that and operator the n bit wise Operator we can have up to eight flags per byte.
So I don't know if you're making a program this special flag could maybe represent that the file that
the file format your reading is.
I don't know it's read only or something.
So this is incredibly good because it means that when you store when you store char a in the file it
can represent eight different states that are different flags.
So I could literally have a lesson designing a file format.
I could have a bite in the file that represents attributes of this special file format that we're making.
And then you can see that this special flag if this final bit here is set in the file then you know
that this special flag set and we can do whatever that special flag was supposed to do.
So essentially without this we would have to store eight characters.
So eight bytes in the file to represent each of the eight flags.
Whereas by using bit wise operators we only need one bite and we can have each individual bit represent
a flag.
So back to this calculator less.
Now a select binary again unless.
Now explain that all a bit wise operator.
So if I do one two three four one two three four one two three four one two three four.
And now I now press four and I press calculate I can see the result is equal to to both of those numbers.
Now let's change on binary digit to zero doesn't matter which one we now press calculate we still see
our original result and the reason for that is because bit wise all it checks the bit against this bit
and if either one of them is one then the resulting bit is one.
So if these are both zero and I press calculate we see that.
But if this one zero here but this one here is still one then we still see the same number.
If I now change this one to zero press calculate we now see zero.
So once again the process of checks that this number here is one or this number is 1.
And if either one if either of those is one then that resulting bit is also one.
So let's now use our bit wise or in our program.
So firstly give it to this if statement and put a zero here instead of a one.
So this is decimal to FIFO which can be seen by doing this we now compile that we can see that this
binary number is equivalent to change in fifty five forward decimal.
So what if we want to set a special flag in a while a bit wise or operatives great for that we can literally
go a equals a special flag.
So you use this little line to represent the it wise all.
So if we can pull out now we can see 2 5 5.
So what's happened is it's done a bit wise.
All on a special flag.
So the process has gone through and it said OK we see a zero here in our special flag.
Is there one in a.
Yes there is.
So this resulting bit is one and then it is the same here.
Is this one.
No it isn't.
Is this one.
Yes it is.
And this goes on and on.
Until our final bid and where a final bit now and we say is this one.
Yes it is.
Is this one.
No it isn't but because this is a bit wise all and this is one then the final result bit must be one
and that equals to two hundred fifty five decimal.
So that is how you can do the bit wise all in C.
I should also mention you can also append it like this or equals.
So that says we're changing variable a and we're going to do a bit wise or our special flag.
So that basically the same the same A equals a bit wise or special flag.
It's exactly the same.
Much like you can do plus equals or minus equals when you're dealing with normal numbers and not bit
wise operations but we're now going to discuss it.
Why is XOR now what XOR does if the bet is not equal to the other one then the result is 1.
So for example if all of these were zeros one two three four one two three four eight zeros we first
calculate we see all ones if this one is changed to one we see this one becomes zero because both of
those are one if this one becomes zero we can see this one become zero because this bit and this bit
zero.
So that's what XOR is.
So I've created two chars here to represent these binary numbers and we're gonna perform an XOR on them.
So go unsigned char result equals a B.
So just choose this little arrow character here to represent that and that is our XOR operator.
So I can now put the result
and we should see decimal one which we do we see decimal one.
So what happens is our result is equal to zero B one two three four one two three four.
Get did finally put a one on the end.
So one two three four five six seven zeros and a one on the end in binary.
That's what is equal to and this binary number is equivalent to decimal 1.
So that is basically the X in action.
So what's happening is this going here.
Is this one.
Yes.
Is this one.
Yes.
OK.
Because they're both the same.
That's a zero.
So then a zero goes there.
And it does that seven times and it comes to our final bit and it says is this the same as the one in
B.
No it isn't.
So the result must be a one.
So that is basically the bit.
Why is X.
So let's now discuss bit why is not Saul a bit why not does is it basically flips the bets.
So if we did one two three four one two three four and we press compute we get zeros.
If we did one two three four one two three four we get 0 0 0 0 1 1 1 1.
So basically the bit if the bits one it becomes zero.
If the bet 0 becomes 1 and this is done on one of her ends there isn't to like there is with all the
other operators I've just shown.
So that is basically it was not.
So I'm now going to show you how to perform of it why is not in C all we do is is scared of this we
go a unsigned char result equals and then this little swirly character and an A.
That's all it does.
So this will flip the bets.
So the result will be equal to one two three four one two three one.
That's what the result will be equal to it'll flip the bets.
So we should see decimal 1 I put it here.
Let's try that
compiler.
And we see decimal 1 so let's show a bit why not.
Let's now explain pitch shifting.
So essentially you can shift bits to the left or to the right.
And that's pretty cool.
So let me show you how to do that.
All we do is we say result equals a and we do two left arrows zero B one two three four one two three
four.
Put a one at the end.
What that says is shift a one to the left.
That's what I says.
So if we now compile up you can see 2 5 to
do that
2 4 8 so this shifts the bets to the left.
That's basically what's happening here.
And you can also shift to the right by doing two right arrows compiler sixty.
So that is pitch shifting.
It basically allows you to shift the bets to the left or to the right.
So I express this as binary but you can use one here as well.
So this means shift one bit to the right.
This means shift one bit to the left.
So let me show you how to do that with the bit Y's shift calculator on line just so just so you can
understand it a bit more.
So we have four ones here eight ones here we have and we want to shift one to the left so press shift
we can see the binary is now one two three four five six seven eight nine is now nine bits in length
because we shifted one to the left but in the process it doesn't quite work like that.
If you if you overflow the register size the basically the size the maximum size that you can store
the data then that is discarded.
So all we would see is these that I've selected here.
So essentially by sort by shifting all of that to the left by one we actually end up with a zero as
our first bit here.
And that is what happens because we've shifted it.
And the final bit is discarded if you shift to the right.
Then you'll notice we have one two three four five six seven.
Only seven bits now.
So likewise there would be a zero here that you can't see on this calculator but that's how it would
look.
So if I just get up notepad quickly it would be that.
So I shifted to the right by one.
We lose our final bit here at the top and that becomes zero.