 What's up guys, Mike the Coder here. Today we're going to go over the problem of gray code. I'm going to skip coin piles and problem reorder. I'll come back to that later. But for now, let's just do great. So a gray code is a list of all two to the end bit strings of length and any two successful strings differs exactly one bit. Your task is to create a gray code for a given length and so if you look at here what they mean right now I'm actually in California and I don't have my I did not bring my microphone so I'm just gonna have to do whatever I can. Okay, so we're given the length to and we want to write the gray code. So in this case what they're doing is they're just listing all the different binary values of two. So it just be like 0000 and 01 right and then if you plus one again you get 10. So each of these values could be each of these places could be either one or zero right so this 00 let's make this into one and then here let's make the second one into one and let's make the last one into a one also. So those are those are all the values you have you have 00001 10. Okay, that's essentially what they're doing and then let's say we have the value three you're gonna have you 0000 right and we have to flip each of them into one or zero so let's flip this last place first so then we have 001. Okay, and then let's let's flip this one now let's flip the center one so we have 010. Let's flip this last one 011. Let's flip this one the third one here and one let's flip all these back to zero so 00. Okay, I hope you guys understand what I'm doing here. Then we have this one we'll flip this one let's with the last one 101 and let's flip this one 110 and then we have 11. Okay, so essentially what we're doing is we're going to go to each of the bits and flip them. Okay, so yeah and we just print them out. So here we have a total of two to three here for this one it's two to the three so 12345678 so two to three which is eight and then for this one it's two to the two because there's two to two and that'll give two to four so yeah. Okay, so how do you do this in code? One way you could do it is that you go through every single bit so since there are n bits here right there's n choices here we go through all two to the three bits right and then for each of them we're just going to shift it right for each of these bits we're going to shift to right and then flip that one bit at the end towards that so what I mean is like how do I explain this? I think so I'll show you guys why I'm just looking at my code real quick to see so you could shift it right and then you could XOR because XORing would flip the last values again let's just see what you see right yeah yeah so he's right it okay so so what we're going to do is we're going to go through each of these values each of the bits so let's say I'm on the first bit so the first bit so what I'm going to do is I'm going to loop from one to two right and then what I'm going to do is I'm going to convert this one into like a binary number right so let's convert this one into a binary number so let's say let's see what is one's binary zero zero zero one okay and what I'm going to do is I'm going to XOR this so XOR is basically just like flipping all the other bits I'm going to shift this binary value of zero zero one to the right so when you shift everything to the right essentially is that you're just adding more zeros in the front so this one becomes a zero so then we have zero zero zero and then if you XOR them essentially is that you're going to get the you're going to get you're going to flip those values back flip the last bit all those last bits are going to get flipped and then you're going to have the next value to print out so here if we flip if we shifted one to the right and we flipped it um one XOR zero is still going to be one right because the bits of XOR is like that um zero zero zero zero zero zero okay so then this would just print out one okay now um when we get to like as we get higher and higher bits um so let's say it was at like two and then three and then four so let's say it was like um I don't know so let's say we're at two so I'm going to erase this actually so we already did one so since I'm moving from zero two to three so that would have displayed zero zero one now let's say we're at we were at two so what's two's binary so two's binary is uh zero one zero so two is zero two the one yeah zero one zero and then I'm going to shift this right uh bits to the right so if I shift it to the right um so shifting one bit to the right would be this one would go to the right side and then all the rest would be zero so this zero is gone right this zero is gone and then it would be one zero okay so I'll just write the next bit shift here one zero zero so then after I bit shift it to right and then I XOR it zero one becomes one one zero becomes one zero zero becomes zero and this would be the next bit of adding one to the to the right side of this so essentially what we're doing is that we're gonna we're basically flipping the last bit into one okay and then uh for each of the values from two to the two to n two to n so because if we shift it right shift the last bit to the right by one and XOR um all the values if it was one all the values get added by one okay in order to to display it so if you look at the pattern in tier of three you'll realize that like the pattern of these sides basically what they're doing is they're just flipping the last bit into one and if it's one it flips to zero and then it goes to the next one and flips last one and one into zero so on so forth so essentially what we're doing is that if we shift it right by one shift the number right by one and then we XOR it we're gonna we're basically changing each of the bits the last bit to turning it on or off for each of values okay so then if we loop from one uh loop from zero to two to the n zero to two to the n and we do it for all these values all the values from i from zero to two to the n um what we're doing is that we're basically flipping each of the last bits over and over again and then in the end we're gonna have all the actual bits there that are correct okay so yeah if you essentially shifting right by one and XORing it um you're gonna get whoops you're gonna get uh each of the bits are gonna be uh the last bit is gonna get flipped so as you can see here right to this last bit the zero got flipped into one here so yeah once we do that for all the numbers from zero to two to the end it'll display all the last bits that are flipped so here's the code um we loop from i equals zero and go up to two to the n and do i plus plus then um our value what i do is i just take the current value of i i XOR it by i shifting right by one and then what i'm going to do is i'm actually gonna you know because i don't want to like go through every bits and print out if it's on or off right the last bit is on or off i just put this this uh this number this value for the bits um if you just set equal to it like a number of variable what it does is actually it actually converts it back to base 10 so this is now an actual number and then what i can do is i could put this number back into like a bit set so then i could convert it back into binary i know it's kind of crazy but like what this is doing is that this gets you your bits right after you shift it right and then XOR you get your new bits of zero one one right so now what i'm doing is i'm converting this back into like a number so it would be like two to the zero one times two to the zero plus one times two to the one plus um yeah so it would be zero times zero one two right so the zero times two to the two plus one times two to the one plus one times two to the zero okay plus one times two to the zero right so what i'm doing is when you set it equal to a variable long long value is equal to this it converts it back to a number right so this would become like a number it would be like equal to two plus one which is three this converts back to number three and then what i could do is i could put into this uh bit set whoops my bad i could put into a bit set and then what i could do is i could call two strings so then i could just print out all the bits okay so i don't have to like check if like each of the number of bits is like one or zero and they're printed out right so this is like like an easier way cover into a number put into a bit set and then cover into a string okay then here um i'm going to loop from i is equal to backwards i'm looping backwards essentially because bit sets are weird in that the bits like if you wrote to loop um in order to print it out in the proper way um you have if you like it's weird in that this one one is actually opposite it's like kind of flipped right so what i did was i loop backwards from 32 to uh 32 minus n and i is less than 32 and i plus plus right and the reason why i did this is because uh yeah it's because for some reason it's backwards so yeah i just printed it out um print out the bits backwards okay um yeah and then i just printed a new line here um the reason why it's 32 is because there's 32 bits in an integer i believe yeah so one integer is four bytes and there's eight bits in one byte so there's 32 bits okay so yeah that's essentially the gist of this problem i hope you guys enjoyed this video right come subscribe i'll check you guys later peace