 What's up guys? Mine is Michael and welcome to my YouTube channel today. We are going to go over how to get the most significant bit of a number So let's say we have say we have the number of 10, right? This is in base 10 and I want to get the most significant bit of this number And what does that mean if you were to convert 10 into binary 10 to binary? It's 1010 right and the most significant bit is the bit on the far left side. So it's this one This is the most significant bit right this one is the most significant bit This most significant bit if you were to actually draw out the number of powers So the powers in binary starts from 0 right 2 to the 0 power 2 to the 1st 2 to the 2nd 2 to the 3rd Right if you were to you know put all these values into its equivalent binary forms have this 1 times 2 to the 3rd plus 0 times 2 to 2nd plus 1 times 2 to the 1st plus 0 times 0th right You would see that the most significant bit is actually 2 to the 3rd which is equal to 8 Okay, the most significant bit is this 2 to the 3rd right this part this part this one corresponds to 2 to the 3rd and That most significant bit is 8 right so Basically what they what we want to do is basically to find the most significant bit of this value and to compute that So let's actually go over how to do that now because might as well do it So there's a there's actually a few ways you could do it There's actually three ways you could do it But um, yeah, let's actually go over all three our guys So the first efficient way to do this or not actually not efficient way To do this is actually to just keep dividing them the number by two and then keep a count Right and then in the end by the end of the loop What's gonna happen is you're going to have a counter that counted to how many times this number was divided by two And then we just raise that to the power, right? That's what that's basically how you would do it. So um, So basically We have ten let's say we have ten here and we're gonna divide it by two So this is gonna give us five so in our counter is going to become One right so we add one to the counter, right? Then we take five and we divide it by two again and then because this is like a floor it rounds downwards, right? Remember division renegers Trunk hates the last few digits, right? This is gonna go give us two right it rounds downwards So then our account is going to become two Right because we added one here, and then we take two and divided by two and we get one then our count becomes three Right, we added one to the count and then we take one and divided by two And this is gonna give us to zero right because it rounds downwards Then it counts becomes four Okay, so then after the end of this So now we have the counters of the ones that divided by two each time so now remember because our Values of bits are actually indexed from zero, right? So we have two to the zero to do the first two to the second two to the third In the end, we actually have to just take our counter which there's four because we're gonna take this four Right, and we're gonna subtract it by one to get three and then we just take this Three and then just raise it to the third right so two to the third We take this three and then we raise two to its power to the third which will give us eight Right, so that's basically how you would get the kth bit from not the kth bit The most significant bit on the farthest left side from doing this the naïve's approach Okay, so that's how you would do it And I'll just show you guys a code how to code it up. All right guys, so here's the code basically We have a function called set bit number This is just the most significant bit. Okay, so we have our integer n If n is equal to zero, we just return zero cuz like why even divide by two right if it's a zero at that point It doesn't matter. All right, so now we're gonna have a counter. I call it MSB, but um, yeah, you could call it count It doesn't matter Basically, we just have a wire loop that checks while n is not equal to zero We're just gonna keep dividing by two. So we just take n and divide by two So as many times as possible, right and then each time we divided by two We're gonna add one to the most significant bit and then in the end we just take this Most significant bit that we have subtracted by one and then we're gonna do two to the power of that And remember whenever you bit shift left we take a value of one, right and we shift it To a left that number of times this this does raise it raises a two to the end, right? So this is going to raise it two to the Number of bits minus one so in our case when it was ten right for ten We had four and we take four minus one Gets us three and then we just this is gonna do two to the third power which gives us eight So let's say we could actually step through that right here. Okay. Um, yeah, I'll just put the here Okay, so as I can step through it to explain how the code works I don't know so let's go to the debugger. Okay, so initially we have ten, right? my number is ten and then the The value of our bit What's an efficient bit? Why did it? Why does it say to it should be ten? Oh, that's weird. Okay, whatever So yeah, what's an if we can bit is zero Right cuz we started zero and then while it's not equal to zero While n is not equal to zero my bad. Well, so this while this ten is not equal to zero Which is keep divided by two so it becomes five and we add one to our most significant bit Which is one and then now we divided by two again So we have two right we and then that is now become two and then we add one to our most significant bit and then We divided by two again, which gets us n equal to one and then we add one to a most significant bit to get three Then we add one again. I Yeah, divide by two again, which gets you zero and added one to his before in the end we're gonna return a one shift left to Four minus one, which is three right so four minus one is three and then we shift left to that So it's gonna raise it two to the third power, which is going to give us Eight so most significant bit returns eight and then if you in the end you just get eight here so that's basically the answer given ten and now I'll show you another way that's not o of n time complexity but it's a bit more way more complicated and Yeah, I'll just explain that other second way. All right guys. So now what we're gonna do is we are going to do the Most efficient solution, but it's actually more confusing. So I'm not sure if I would actually use this Lately so basically what we're gonna do for this most efficient solution of over one We're not actually gonna check any conditionals. So how this works is that we're going to keep shifting it right? by one two Four eight and then sixteen and the reason why we go up to sixteen is because we know that in an integer an integer There's 32 bits Right, so then the highest value you could have is probably is sixteen bits because you're starting from you're starting from zero, right? So if you're going to keep shifting Keep shifting right by one two four eight sixteen What's going to happen is is that and if you or all of them You're going to have a bunch of ones and then the rest are zeros, right? The rest are zeros and then what's going to happen is that if you shift it right by a if you add Yeah, you're basically what's going to happen. You have a bunch of ones if you add one You're going to have the first value of most significant bit is gonna be one and the rest are zeros And then if you shift it right by one, they'll be your answer. Okay? So I'll just actually explain how this works, so let's say we have one zero one zero, which is ten Now what we're going to do is we're going to shift this ten to the right by one And what is this going to do is we are if you shift this right of ten one zero one zero you shift this right by ten by one? I maybe I should put it in binary. So yeah, if you shift it right by ten by one What's going to happen is it's going to give you So this this one because of this zero goes away, right? This this this part goes away. So then you're going to have Zero one zero one okay This is this is the answer and what we're going to do is we are going to or both of these together So if you or both of these together, you're going to have one zero one zero two right or it with zero one zero one or Right and then zero one zero one becomes one one zero becomes One one zero becomes one one zero becomes one. Okay, you get all ones. Okay? Yeah, you get all ones. Okay, and then after this is going to be one of your answers We're going to all ones. So in the end you're going to have all ones here, right? So this is going to give us One zero one zero two or it with zero one zero one is going to give us all ones Okay Which is here and then what we're going to do is we're going to keep doing this Again, so now we're going to shift this right down by two and then oring it in this case We're actually pretty lucky that we got all ones in the beginning But how what we're going to do is that if you didn't get all ones you just have to keep doing this All right, so let's actually just continue doing this doing this. Okay, so here we're going to now shift it right by two Right. So now we have one one one one if I should one one one one space to write if I shift it right by two So here's going to be shifting right by two. All right. This is going to give us zero zero one one. Okay, and Zero zero one one and how I got this because I just basically Took away the last two values and moved it right And now I'm going to or it with one one one one zero zero one one And if you or these two together, you're going to actually end up. Let's actually just do or or or these two get together You're going to get all ones. Okay All right, and then now we have to do four and then 16 so I think you guys understand just of this basically you just have to get all ones by the time so Ideally in the end. What's what's going to do is you're going to have all ones. Okay, um, so After doing this two four Eight and then 16 you're going to end up with all one. So let's just assume that we did all the all the shifting already and got all ones Okay, so now at this point, it's all ones Right at this point. We're assuming that we already shifted over right by two and then four and then eight and 16 right so after shifting at all these You got ones, right? We got all ones. So now now we have this This binary representation all ones. So now what we're going to do is we're actually going to add one to this binary representation and the reason why we add one to this binary representation is that it's going to basically flip all the bits to To become zeros and then it's going to have the most value at the opposite of one So if if I add one to this answer all the bits are just going to become zero zero zero Zero right and I'm going to have a one on the farthest left Bit, okay. This is going to be on the farthest left bit. It's all ones Right one and then all ones here all zeros a one year and then all zeros here Now what I'm going to do is I'm going to basically just shift this right by one. So now this one be goes to Becomes this position the one on this side becomes this position and then we get Zero one zero zero zero, okay, and then as you could see here this one this value of one Is the same most significant bid position of this? Okay, right because we have ones and all zeros and the reason why this works is baby, baby mainly because After shifting all these values by one two four eight and sixteen All these bits become ones and then when you add one you basically add You attach a one on the farthest left and then all the the rest becomes zeros and then if you shift it right by one That that gives you the most significant bit on this left side, right? Okay, yeah, that's basically how it works that's basically how the gist of it So now let's just explain the code and just show you guys what the code does, okay? So but this is basically the code So I take the original number of n and I shift it right by one and I'm gonna or it right with then Then I'm going to take the value of n shift it right by two then I'm gonna or it And then I'm gonna do the same thing by four or at Eight or it and then sixteen shift it right by sixteen and then or it so then in the end What I'm gonna what's gonna happen is I'm gonna have all ones in all the places of where it is, right? Now if I add one that's going to make them all zeros and put the one on the furthest left side Okay, all the way on an extra one on the furthest left side and then basically if you shift it right by one That one on the furthest left side ends up becoming the Most significant bit number. Okay, so let's I'll actually just step into the code for you with you guys So here I'm I press this number and let's just see we I still put 10 as the original one so We have 10 we shift it right by or it by one arms. This actually gives us 15. That's what that's actually insane I didn't even know that Is there a way to check the bits? I don't even know actually maybe I could look at the assembly look at the bits No, I can't whatever Shift it right by two Four okay eight 16. Yeah, so as you can see it's all values become ones and that's like you actually gives us a gave us a value of 15 If you add one you now you become 16 and then if you shift it right You're dividing by two and that actually gives you eight So the answer is eight. Okay, so yeah, that's pretty much how you would find the most significant bit Of a number So let me just reiterate what we did. We just kept shift shifting right since we know we there's 32 bits in an integer the maximum we probably have is 16 and then if you were just to keep shifting right by powers of 1 2 4 8 and 16 You're going to get all values of ones right all values of ones In all the locations then if you were to add one to it You're going to have a one on the furthest left side and then a bunch of zeros for the rest of these But the position of this one is one one greater, right? It's one greater And we have like extra zeros here So what we do is in the end we just shift it right by one to actually get the right number of values of zeros and One so then this is actually this one this value ends up actually becoming the right Location of the bit shifting. Okay, so yeah, that's pretty much how you would do this problem Raid com subscribe. Hope you guys enjoyed this video. I'll check you guys later. Peace