Find Rightmost Different Bit Between Two Numbers
Duration: 6 min
This video lesson is available to enrolled students.
Inside: a video lesson and guided study material.
Module outline
- Quantitative Ability: Speed Mathematics and Basic Mathematics, Percentage, Ratio and Proportion (Ratios), Average, Profit and Loss, Simple Interest and Compound Interest, Time and Work, Pipe and Cistern, Speed Time and Distance, Number System, HCF LCM, Data Interpretation, Age Problems, Mixture and Alligation, Partnership, Algebra, Sequence and Series (Series), Mensuration and Geometry, Arithmetic
- Reasoning Ability: Puzzle Solving (Puzzles), Seating Arrangements, Coding Decoding, Blood Relations, Directions (Direction Test), Syllogisms, Data Sufficiency, Ranking, Order and Sequence, Series (Number and Letter Series) (Numerical Relations and Reasoning), Logical Sequence Of Words, Calendar, Clock, Cubes & Dices, Analytical Reasoning (Counting Figures Reasoning), Data Arrangement, Non Verbal Reasoning (Spatial Aptitude) (Spatial Reasoning) (Visual Reasoning), Deductive and Inductive Reasoning (Logical Deduction and Induction) (Prepositional Reasoning)
- Verbal Ability: Comprehension / Reading Comprehension / Unseen Passages (Critical Reasoning) (Paragraph Questions), Cloze Test (Cloze Passage), Sentence Re-arrangements (Para Jumbles) (Narrative Sequencing), Vocabulary, Sentence Completion (Fill in the blanks), Active Passive Voice (Voice), Direct and Indirect Speech, Subject Verb Agreement (Verb Noun Agreement), Tenses, Preposition, Articles, Sentence Correction (Error Correction)
- Programming Basics (MCQs + Interview): C Programming by Yash Sir, Programming in C++, Java, Python for Placements by Yash Sir
- OOPs (MCQs + Interview): Object Oriented Programming (OOPS)
- DBMS (MCQs + Interview): Basics of DBMS, ER Diagram, Relational Model & Functional Dependencies, Keys & Integrity Constraints, Normalization (1NF - BCNF), Transaction Management, Decomposition Properties & 4NF, Concurrency Control, Database Recovery, File Organization & Indexing, Relational Algebra, Database Security & Authorization
- SQL (MCQs + Interview): SQL
- Operating Systems (MCQs + Interview): Introduction to OS, Process Management, CPU Scheduling, Deadlock, Threads & Process Creation, Memory Management, Virtual Memory, Process Synchronization, Disc Scheduling, File Management, Security
- Computer Networks (MCQs + Interview): Introduction to CN, Data Communication, DLL: Access Control, DLL: Flow Control, DLL: Error Control, DLL: Framing, Data Link Layer - Ethernet, Net Layer: IPv4 & Proto, Net Layer: IP Addressing, Net Layer:Routing Protocol, Transport Layer Services, TL: Congestion & UDP, Application Layer, Hardware basics, Network Security, Cloud Computing and IoT, Cyber and Info Security
- Coding: Coding Questions by Yash Sir
- HR Interview: MR & HR Interview Preparation
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.

