Find Rightmost Different Bit Between Two Numbers

Duration: 6 min

This video lesson is available to enrolled students.

Return to /learn/TCS-NQT-PREPARATION/advanced-coding/coding-questions/coding-question/asset-find-rightmost-different-bit-between-two-numbers after enrolling

Inside: a video lesson and guided study material.

Module outline

  1. TCS One Shot Free Videos & Notes: TCS Preparation
  2. Numerical Ability (Foundation Section): Ratio and Proportion (Ratios), Divisibility Rules, Data Interpretation, Mixture and Alligation, Permutation and Combination, Logarithm, Number System, HCF LCM, Sequence and Series (Series), Speed Time and Distance, Percentage, Average, Powers and Exponents (Surds and Indices), Algebra, Probability, Profit and Loss, Simple Interest and Compound Interest, Time and Work, Pipe and Cistern, Mensuration and Geometry, Mental Ability, Age Problems, Statistics, Arithmetic, Partnership, Speed Mathematics and Basic Mathematics
  3. Reasoning Ability (Foundation Section): Series (Number and Letter Series) (Numerical Relations and Reasoning), Coding Decoding, Data Sufficiency, Non Verbal Reasoning (Spatial Aptitude) (Spatial Reasoning) (Visual Reasoning), Syllogisms, Deductive and Inductive Reasoning (Logical Deduction and Induction) (Prepositional Reasoning), Seating Arrangements, Blood Relations, Directions (Direction Test), Analytical Reasoning (Counting Figures Reasoning), Puzzle Solving (Puzzles), Calendar, Clock, Cubes & Dices, Selection Decision Table (Decision Making), Crypt Arithmetic, Venn Diagram, Data Arrangement, Inequality, Analogy, Ranking, Order and Sequence, Logical Sequence Of Words
  4. New Pattern-Verbal Ability (Foundation Section): Sentence Completion (Fill in the blanks), English Writing Ability
  5. Old Pattern-Verbal Ability (Foundation Section): Vocabulary, Noun, Subject Verb Agreement (Verb Noun Agreement), Adjectives, Tenses, Pronoun, Preposition, Articles, Direct and Indirect Speech, Conjunction, Modals, Sentence Re-arrangements (Para Jumbles) (Narrative Sequencing), Sentence Correction (Error Correction), Comprehension / Reading Comprehension / Unseen Passages (Critical Reasoning) (Paragraph Questions), Verbal Analogy (Word Based Analogy), Active Passive Voice (Voice), Determiner, Facts, Inference and Judgements, Adverb, Cloze Test (Cloze Passage), Sentence Construction, Verb
  6. Advanced Quantitative Ability: TCS NQT Advanced Quantitative Ability
  7. Advanced Reasoning Ability: TCS NQT Advanced Reasoning Ability
  8. Advanced Coding: Coding Questions by Yash Sir, C Programming by Yash Sir, Python for Placements by Yash Sir, DSA using Python, Object Oriented Programming (OOPS), Programming in C++, Java
  9. TCS Full Length Mock Test:
  10. TCS Interview & Resume Prep: MR & HR Interview Preparation, TCS Interview Preparation, Mock Interview, Interview Recordings, Resume Preparation, Resume Review
  11. Mega Test:
  12. Grand Test:
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

  1. 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. 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.

  3. 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.

Loading lesson…