 What's up guys my name is Michael and welcome to my YouTube channel today. We are going to basically Do this problem where you try to detect if two numbers have the opposite sign or not Okay, using bit masks. So if I given you a number like Three and negative three Tell me if they have opposite signs or not and we have to use it only with bit masks So that's basically the problem Okay, so the thing is is that um, this is actually pretty easy If you think about it. So first of all, um How What is a Sign or an opposite sign even mean, right? What does a negative sign mean in bit masks? Okay, so let's say you have three A negative three so let's say we have three and negative three. So if you were to basically, um Convert three into bit masks. You have zero one one zero zero one one right because you have zero which is one Two to the zero power is one plus two to the first power is two, right? So this gets you This gets you to zero zero one one. Okay, so that's three. Okay. Okay. All right. Um Basically in computers if if a sign is negative What they normally do is they would um They would flip the bits right and um Add one Right, so they would have a do two's complements, which is zero zero Uh, so this is going to get you one one zero zero you add one. So then you have one one zero one As you can see when you flip all the bits and then you add one So this is the two's complement of three, right? So this is the This is the values Of the inverse, right? So if you were to add three and negative three you get zero, right? Because all these bits would end up becoming um zero, right? If you add this bit These bits and this bit these bits, right? If you add them up together Add them up. You're all you're going to get zero, right? So this is two's complement. All right So as you can see from here using, um The bits the computer represents the first bit for For negative numbers. It's always a one, right? Because this um this would represent a negative number for that and for positive numbers. It's normally like zero or Actually, it's not normally zero actually, but uh for negative numbers. There's normally a one that represents positive or negative Inside the whole number system So if you were to XOR both of these like so if you were at XOR both of these values, um All of the values are end up going to become Um ones Right, all these values are going to become one. So all the all the positives Yeah, all these values are going to end up becoming ones, right? So all the zeros and zero so if it's different signs all them the first values would become ones So then you're going to if you XOR both of these, right? You would get um zero and one would become one zero and one one become one One zero becomes one and then uh except for the this this value one one would become zero, okay? So you would get this number uh one one one zero, okay So now all you have to do is just check Does this Is this value, right? Is this um does it have a one in the first place, right in the first place? Is this value negative, right? So normally if if a value is negative it would have a one on the far left's corner and values in um, binary Right in representation from two's complement, right? So, um, if there is one if this Binary representation is negative. So it's less than zero then pretty much, you know, you have opposite signs So all you have to do basically is basically Take both of these numbers and XOR them and then you check if this your end result is less than zero because then it would have a Number here at the front. All right, so if they were They've had opposite signs Then there is there's going to be a one in the front. So you just check if it's negative So if it's less than zero then that's the case, okay? Um, if they don't have opposite signs, you know, it's not the scenario So basically to detect if they're both If they both have opposite signs or not, you have to just um You XOR both of them and then you check if the first leading value here is a one or not. Okay All right. Uh, another way to do it is the same thing as checking XORing them both but it's to check if this value has a one or not another way you could do is you actually could shift it right by 32, uh, 31 bits to the right Because we know an integer has they at most 32 bits, right? So an integer has at most 32 bits So if you were to shift all the 31 bits to the right, so you have this number 31 bits, right? So that's going to get rid of all these values Right besides the last value in the first place this one here So then we don't actually have to compare and check if it's less than zero We just have to check if after shifting it right down by 31 bits Is our last bit set and if it is set then that means they have opposite signs if it's not then it doesn't Okay, so I'm going to actually show you the code now for both of the our guys. So here's the code For it. Um, so the first way to do it is just to XOR them both external X or Y and then um, basically if the leading value is a one and to check that All you have to do is check if it's negative or not Because if it's negative that means the leading value of the first bit is one So you just check if it's negative. So to do that, you just check if it's less than zero If it's less than zero, then that means that it's negative. So that means that they do have opposite signs Right, because we have a leading bit of one. Okay All right. So the other way to do it is just to X or X X or Y and then you shift it right by 31 bits If you shift it right by 31 bits to the right in the end, um, that's going to get you the last bit the first First most bit the first most bit on the first value Of your result, right the first most bit of one And uh, if that is the case if it equals to one, then that means they have opposite signs Otherwise they don't right because basically both of both of these are just checking if the first bit value that was set is actually equal to one Uh, and then that means they have opposite signs because when you XOR them, right? If they do have opposite signs, that means there's there's like when you XOR them the end result is a negative value So there has a first bit that is set that is one So yeah, um Here I just test it in the main method I just created a variable of X equal three and Y equal negative three And I just print if it has opposite signs or not. So in this case if you run it It's going to say opposite signs. Yeah, because three and negative three have opposite signs And running it the X or value does work So that's basically how you do this problem rate com subscribe. I'll check you guys later. Peace