 What's up guys, my name is Michael and welcome to my YouTube channel today We are going to go over how to check if a number is odd or even using bit mass Let's say we have the number 10 again, which is in base 10 we convert into binary We see that you get 1 0 1 0 if you see this in let's say we subtract 2 from 10 So we get 8. This is an even number right 8. Well, what do we have? Well, if you were to convert 8 into binary would have 1 0 0 0 and let's say we had like 6 in binary Binary so what we have 0 1 1 0 Let's say we have 4 in binary to 0 1 0 0 and let's say we have 2 in binary. Okay, 0 1 Yeah, okay, so as you can see here if almost all all the even numbers have a 0 at the end Okay, all the even numbers have a 0 at the end just based on Looking at the bits right looking at the bits So if I want to check if a number is even or odd, how would I check it? Well, we just have to check if there's a 0 in the end and that would be an even number So now recall this property in mathematics, which is like any odd number can be represented by as an even number So if I have like an odd number n right, this is an odd number Let's say this is an odd number It could be represented by 2k plus 1 Okay, and 2k where k is an even number, right? So if you but this is basically just saying that if you take an any even number you add it by 1 That's gonna get you an odd number, right? And yeah, so any odd number can be represented by an even number Which is any an even number plus 1, right? And even numbers anything that's divisible by 2 Right see right here to the divisible by 2. So this is basically every odd number so if you were to add 1 to Basically any even number like let's say we check 11, right? 11's binary is 1 0 1 1 Right in base 2 and if you add 1 to any odd number Right, so let's add out and add 1 to an even number like 8 8 plus 1 is 9 So what is 9's binary 1 0 0 1, right? So as you could see here For even numbers They all end at 0 right all and at 0 all the easier here and for odd numbers. They always end at 1 Last ending is 1 Okay, so if you want to check if something is an odd number all you have to do is just check if is the last value a 1 right? Otherwise it must be an even value Right, so it is the last bit this last bit. Is it a 1? So if it's a 1 then that means it is Odd otherwise, it's an even number, right because it would have zeros, okay? So how do you check if it's something is even or odd? Well simple if we just want to check the last place is Is to check it as a 1 or not this last place of this one All we have to do is just and it by 1 so if we were to go back This is 11 So if I end it by 1 so that means I end it by 1 so all these places are zeros and have one here Right, basically if you end it by 1 all the rest of the bits Become zeros anything in front of the last bit here becomes zero so they all these values become zero and Little bit plus adds a last bit, right? So if your last bit is 1 is a 1 it's going to equal to a 1 right and Basically, if you just end it by 1 and your and you last bit is 1 and it equals to 1 That's going to be mean. It's odd. It's an odd number Otherwise, it's otherwise. It's even So that's basically the gist of this code. So Let's just code it up now. Alright guys, so this is basically the code I just read in a number from the user input and I just check if I end it by 1 and it's equal to 1 then It's odd. Otherwise even Technically, we don't even need this we could just get rid of the equal equal 1 because in C++ anything that is If it if it's greater than zero, right, it assumes it's like it's true, right? So the the conditional would be true. So this would be true if that's the case But I just put this just in case. So we should put parentheses So if I end it by 1 is equal to 1 then it's odd. Otherwise even so here Let's just run it. So let's say I put in like, I don't know 8 8 that's even. Yeah, so that's right. Let's put 9 That's odd. Yeah, that's right. Okay. Let's try 1 1 is odd Yeah, so that's basically the gist of the code. Hope you guys enjoy this quick video where you can't subscribe I'll check you guys later. Peace