Find Rightmost Different Bit Between Two Numbers
Duration: 6 min
This video lesson is available to enrolled students.
AI summary & chapters
AI Summary
An AI-generated summary of this video lecture.
This video is a programming tutorial that teaches how to find the position of the rightmost different bit between two integers. The instructor first presents the problem statement, which involves finding the rightmost bit where the binary representations of two numbers, M and N, differ. He then explains the core concept: the XOR operation between M and N produces a number where only the differing bits are set to 1. The key insight is that the rightmost set bit in this XOR result corresponds to the rightmost different bit in the original numbers. The solution leverages the bit manipulation trick `n & -n` to isolate the rightmost set bit, and then uses the logarithm base 2 to find its position. The final solution is implemented in C, with a function `getRightMostSetBit` that calculates the position, and a main function that reads the input and calls this function. The video concludes with a live coding demonstration in an online IDE, showing the code being compiled and executed.
Chapters
0:00 – 2:00 00:00-02:00
The video begins with the Knowledge Gate logo, followed by a slide that introduces the problem. The task is to find the position of the rightmost different bit in the binary representation of two given numbers, M and N. The input is two space-separated integers, and the output is the position of this bit. The constraints are that both M and N are between 1 and 103. An example is provided: for inputs 11 and 9, the output is 2. The instructor, Yash Jain, is visible in a small window, and the video is copyrighted by Knowledge Gate Eduventures.
2:00 – 5:00 02:00-05:00
The instructor explains the solution step-by-step. He first converts the example numbers, 11 and 9, to binary: 11 is 1011 and 9 is 1001. He then demonstrates that the XOR of these two numbers (1011 ^ 1001) results in 0010, which has a single set bit at position 2 (counting from the right, starting at 1). He explains that the XOR operation highlights the differing bits. He then introduces the key formula: to find the position of the rightmost set bit in a number `n`, one can use `log2(n & -n) + 1`. He writes the formula `A'B + AB` for the XOR truth table and explains that the rightmost set bit in the XOR result is the answer. He also notes that the rightmost bit of an odd number is 1 and of an even number is 0, which is a property of the numbers being compared.
5:00 – 6:25 05:00-06:25
The instructor presents the final C code solution. He defines a function `getRightMostSetBit(int n)` that returns the position of the rightmost set bit using the formula `log2(n & -n) + 1`. He then defines `posOfRightMostDiffBit(int m, int n)` which returns `getRightMostSetBit(m ^ n)`. The `main` function reads the two integers, calls the function, and prints the result. The video transitions to a screen recording of an online C compiler (OnlineGDB), where the code is pasted, compiled, and executed. The output for the example input (11, 9) is shown to be 2, confirming the solution.
The video provides a clear, step-by-step tutorial on solving a common bit manipulation problem. It starts with a well-defined problem statement, then builds the solution by first explaining the underlying logic using binary arithmetic and the XOR operation. The core of the lesson is the application of the `n & -n` trick to isolate the rightmost set bit, which is a fundamental concept in bit manipulation. The solution is then translated into a concise and efficient C program. The final demonstration in the online IDE solidifies the learning by showing the code in action, making the entire process from concept to implementation clear and practical for students.