 What's up guys my name is Michael and welcome to my YouTube channel today we are going to go over what X or is and what the operation of X or so let's say that let's say I have a group of people right a group of people who like dogs and I also this is a group of people and I'll just pay dogs right these people are people like dogs and let's say there's a group of people who likes cats okay now I want to tell you would ask you how many people who like cats and dogs okay so how would you do it let's say they're people who have like cats and dogs and you want to group these people together so well what is a good way to do it well normally you would just draw a venn diagram so you would have one side you have other people who like dogs and one side are the people who likes cats right and then whatever's in the middle in this venn diagram this part is going to be the people who like dogs and cats and this is just like from grade school like I have a set of people who like dogs and a set of people who like cats in the intersection inside this venn diagram are going to be the people who like dogs and cats okay so now that we know people who like dogs and like cats well what if I ask you to find the people who like dogs or cats so what do you do well you basically do the same thing you have these people who like dogs but now you ask them what are the people who like dogs or cats well the people who like dogs or cats are going to be the people who like dogs so first of all it's going to be all the shaded area in this side right because they like dogs right so this part of the venn diagram is people who like dogs but it's also the or cats right so either or so it's dogs or cats so that's going to be also including the rest of the shaded area for cats right because you like dogs or cats so this is what dogs or cats is right so this is a set of people who like dogs set of people like cats and dogs or cats is going to be the whole shaded area but now let's say I ask you what if I want to find the people who like dogs or cats but I want to exclude the ones who like both of them so how would you draw that it's going to be the same diagram so it's going to be dogs or cats but what are we going to do is we're going to exclude the ones that are in the center of and right so it's the same thing it's dogs or dogs or cats but we're going to exclude the values that are in both of those of dogs and then these values so this this shaded region right this shaded region right the shaded region region and this shaded region cats right and we're not going to include the ones that are in both of them right so I hope you guys I hope you guys could see what my drawing of this shaded region is so basically the same thing as an or except we're going to exclude the ones started in both of them okay so now let's actually draw a truth table and show you guys what you mean when you have an exclude so let's let's say I have two people right here and this is going to be the this is going to be the dog side and then this is going to be the cat side okay and this is going to involve the exclusive or right dogs and cats okay so let's say you have someone who likes both of them right dogs and cats well we're excluding that out of our set right out of a set of the center so what does that mean so that means that we're not going to include it so it's going to be false right we're not including it that both true and true so they like dogs and they like cats we're not going to include it so it's false right so it's not the center now what if I have someone who likes a dog but hates cats right he likes a dog but he hates the cats so is that inside our set of of exclusive or well if if you like dogs right and you hate cats so you're not you're not going to include anything with the cats but you still have part of the dogs right it's going to be a part of this left side right this left side even though you hate cats you still have a part of the dog of the dogs right so that's that's going to be true right we're just going to include that also right because if you like dogs and hate cats well the dogs are still part of this this left side so that's why it's true now what if you hate dogs but you like cats well it's the same thing right if you hate dogs that means you're not going to include anything on the left side right the value that you hate the person has right is not going to be the dog side because you really don't like it but you like the cats right so that's still part of the set it's still part of the set right because it's still included in this this right part of the cat right because it's you still like cats right even though you hate dogs so even though you're not on the left side it's still part of the set of the values who likes cats so that's why this part is going to be true okay now let's say you hate both of them so you hate dogs and you hate cats well if you hate dogs and you hate cats right that means you're not part of this whole thing at all any of these two right so that means that yeah it's not going to be including into that set the set of people who likes well these so these this is going to be false also so now if you were to convert this into bits where the values of true is equal to one so true is going to equal to one and the values of false is equal to zero your let's actually redraw this true table that we have before so let's actually do that xor and the xor symbol what we normally wrote before in c++ it's this carrot sign normally but you could write xor like that also so let's put every true value going to have one every false value going to have zero so true true and then false to zero true false is true right false true is true and then false false so zero zero is false zero okay so that's basically what the xor symbol does this is basically the bitwise equivalence of xor all right we have a true true we get zero true true false is true false true is true false false is false so that's what that's where they get the whole bit operation for xor in order to understand xor okay so now that now that we have the bit operation of xor let's actually explain um what kind of uh the properties of xor because xor is a huge thing and there's actually properties mathematical properties you could use about xor that will be beneficial all right guys so here's here are a few properties about xor so in um in abstract algebra we call something that is an ambillion group which is like an operation that you could put on a set if it has these four properties and these are actually the four properties that xor has so xor is an ambillion group okay so an ambillion group has these four properties one is it's a commutative the second one is it's associative the third one is that has an identity and the fourth one has a self inverse and we'll actually go over all of these first so let's actually go over the self inverse first because that's actually the most easiest one to go over because that's just showing that it has an self inverse first prop part of self inverse that uh xor does so if you guys know this is the this is the symbol of xor in math right this is a symbol of xor so let's actually just by the way this is a xor by the way so this is a symbol of xor by the way but um basically a self inverse means that if I take a number and I xor it by itself it's going to give me zero and we could actually show an example of why this is the case so let's say I have a number like five right so five and uh this is a base ten and let's say convert into binary okay so what happens if I xor it by itself the bits of five so I'm going to have one zero one and I'm going to xor it right xor it like this one zero one okay so remember guys um one one back in our truth table so let's actually go back to our truth table so why so remember one one is equal to zero one zero is equal to one zero one is one and zero zero is zero okay and a good way to remember this of the bits is that anything that is equal to each other is going to be false right zero and then the rest are just true right the rest are true so um in this case one one one would become zero right because one one is equal to itself so we're going to have zero one one becomes zero zero zero becomes zero right because it's equal to itself so it becomes zero here and it becomes zero and then one one becomes is equal to itself so it becomes zero okay so it's because of this property that um both of these anything with the same values of one one becomes zero right where zero zero becomes zero right it's guaranteed that if you xor anything by itself it's going to be zero okay so this is a self inverse anything you xor by itself is going to become zero okay and yeah anything xor by itself is going to become zero and yeah let's go over the set the other properties now okay so the next property is that xor is and has the identity element so that means that uh any value if I take any number and I xor it by zero it's going to give me the same value so this is the identity property and um if the reason why we do this is um here let's actually go back to the truth table again I have to redraw this so because of this you could see that basically if you xor anything with zero the bits of one and zero you get one so it's still going to be the same value if you xor it with zero and um we could actually show an example back with the five and um if you were to xor this five with zero that's xor with zero so zero is just all bits of zero here right um as you could see uh one zero right one zero it just gets itself with one right so this bit is going to be one okay uh zero zero is the same bit uh bits right zero zero same value of bit so that's why it's going to be always zero remember same values are going to be zero but the other ones are ones right all right uh one zero see one zero is going to be one right because so it's uh different values it's one so in the end you still get the same bits of one zero one right so anything xor with zero you're just going to get the same value okay that's what this identity means so um a good thing cool thing about xor is like if you xor a bunch of numbers if there's duplicate number and then you xor with itself you'll get zero right so then if if you xor zero with the regular number you'll just get the same number so if you use xor you could actually get the only value that's not duplicate so that that's a cool thing you could use about xor right the only value that's not duplicate okay uh let's go over the other properties okay so the next property is a commutative property which basically means that if you xor anything a number with another number right and you flip the order it's still going to be the same thing and we could actually demonstrate this five and six and this is one one zero okay and let's say we xor both of these so we have five and six so we have one zero one there's two xor with uh one one zero so remember back to our truth table we don't actually have to draw the two truth table out again remember the same numbers are just going to become zero the rest are just ones right so this is going to get me zero one one right and uh this number is just uh this is three one plus two is three yeah this is a this three okay so um now let's actually flip the order so remember what we did first was this top one was five we xored five with uh six right this is five six let's flip the order now let's do six xor with uh five so six is just uh one one zero two and then this is five which is one zero one one zero one and let's xor these as you can see the order that you do you still get the same same number uh so um one one these are the same values so it's zero and the rest of one zero is one zero one is one right so these two values are just going to be exactly the same right if you flip the order of xor you still get the same no matter what even if you flip the order it's still the same value the last one is the associated property which is uh this is just saying like if you change the order of parentheses it's kind of like the same thing as the commutative uh commutative so whoops so like uh if i do this right i had a parentheses of this it's going to be the same thing at right if you change the order parentheses it's still the same thing and um we could actually show an example of why this is the case so um let's say i had uh let's let's just make a bunch of bits uh i don't know so they'll let a equal to five b equal to six and c equal to seven okay so if i do um a x or b right so this is going to be i'll actually draw it out the whole thing so a x or b is going to be five x or six right so i i let a equal to five b equal to six c equal seven so five x or six so let's actually try doing our heads now one with a zero is one zero with one is one right because the different values one one is zero because they're the same values and that's what you get now let's take this and x or with the c value right this parentheses right so we're going to take this value and then um so we're going to take this value and x or with c so let's see one one this one with this one becomes a zero so we're going to put a zero this one becomes with this one because zero because they're the same value zero and this zero with a one is a one right zero one they're different values so it's a one okay so this is the number we got for a x or b x or c so this is equal to four right one zero zero two and now let's actually do the opposite the exact opposite right so let's do bc first and then x or with a let's see if we get the same value so i'm actually going to take this size of it but here so i have more room so now we're gonna do is we're gonna do six and seven and then we x or with five okay so now let's do bc first and then x or with a so bc what is bc bc is um zero one is a one one one is zero zero uh one one is also zero yeah so we'll get this zero zero one and now we're going to explore this with a right so now let's do a all right one one gets you to a zero zero zero is a zero one zero is a one okay so in the end you still get the same value of one zero zero two so these values are still the same so no matter what order you do you still get the same associative property okay and um that's pretty much what you need to know about x or pretty much all the properties of x or so now let's talk about some cool things you could do with x or so one is swapping values so you could actually swap two values without using an extra variable and you could do it with x or guys so let's say you want to swap two values a and b so what you could do is you could if you don't want to do use an extra variable you could say um a we're going to set it to a new new value of the values of ax or b then we set b is equal to the values ax or b and then we say a is equal to ax or b okay so this is like kind of esoteric to understand but then i'll explain why this works let's go over an example let's say we have a is equal to five and b is equal to six right and we want to swap these so we want what we want is we want a to become six and then b to become five right this is what we want so how would you do this okay so if i take a and i set it now equal to a x or b right so if i take five and six and i x or them right so now i have one zero becomes one zero one becomes one one one becomes zero this is my new a value right the values that are x or now what i'm going to do is i'm going to take b my new b value is going to be this a value x sorted with b so what what is this going to do right so this new value of b is this new a value so this is this is the new values so i'll put a prime to represent the new values okay so now b so now b is going to equal to a x or b right that we just changed so now we have one zero becomes one one one becomes zero one zero becomes one right so now what did we just do here we completely switched the bits of b right this b is now equal to the same values of the bits as a right so we just switched the bits right because if you x or them x or it again you just switched the bits right that's why this worked we just switched the bits of and b so now um now now what we're going to do is we are going to x or a now this a right this a value we're going to x or it with b with the new b right so here one one becomes zero one zero becomes one zero one becomes one okay so now our new value of a is equal to one one zero which is this is equal to six so our a value just became a b value and one zero one is five and this b value just became our a value so this b value is five so this is this is going to equal to five and these values are switched so this is going to equal this is this is this was originally b's value so these values these values are the same and this value of b is now equal to the value of a so these these values swapped okay so let's let's x explain why this works and what what we basically did okay okay so i'm going to explain why this works okay so what do we do when we do this operation we took originally we we took a and x or b and then we store this new value as the new value of a right so this is the new value of a so the new value of a is equal to a x or b we'll call this a prime a x or b okay now what we do okay so what do we do with the next operation so this part this part of b b is now you're going to equal to a x or b right a x or b so this is going to be the new value of a that we just changed so this b b prime the new value of b is going to equal to the new value of a that we just changed remember we stored it into a right so new value a is a prime and then we explore this new value of a with b right so we take this new value of a a prime which is going to be a x or b and then we explore it with b right that's what this is doing a x or b is x or with b right so this is our new b b prime right and if you were to follow along with the same operations of the properties that i said earlier you could evaluate b with b right anything x or with itself right you could change the order that you're you're you're evaluating this b x or with b is going to give you zero right because anything x or with itself is going to get you zero so you're going to get with a x or with zero okay and then if you x or a with zero anything x or with zero you get itself so it's going to be a so our new b prime our new value b is going to equal to the original value of a okay that's what this is doing all right now what do we do we set our new value of a right so now new value a totally new value of a so we're going to set the a value totally new value of a so this is completely new so this is a totally new value of a right is going to equal to the what we had earlier a x or b x or with um b prime right so the value that we just changed so we just changed b remember we changed b earlier right here so we're going to take the new value of a a x or b and x or with this b prime so this is going to be a new value of a which is a x or b right this new value of a a x or b and then we're going to explore this with b prime so this is b prime the new value b prime which is equal to a okay and then if you were to x or these two these values and evaluate a with x or with a first right anything extra with itself is going to give you zero so you're going to get and with this and this one's going to give you zero x or with b and then anything extra with zero with b is going to get you um the original value of b so you're going to get b so that's why our new values of a are completely new value of a is now equal to b and then our new value of b is now equal to a so we just successfully swapped these two values using the associative properties that we just stated before so yeah this is the reason why swapping work I hope you guys enjoyed this video rate com subscribe i'll check you guys later