Find Rightmost Different Bit Between Two Numbers

Duration: 6 min

This video lesson is available to enrolled students.

Return to /learn/ACCENTURE-PREPARATION/coding-assessment-questions/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. Behavioral Assessment (Psychometric): Behavioral Assessment (Behavioral Competency Profiling) (Psychometric Test)
  2. Cognitive Assessment (Game-Based): Gamified / Game Based Aptitude Selection Decision Table (Decision Making) Puzzle Solving (Puzzles) Non Verbal Reasoning (Spatial Aptitude) (Spatial Reasoning) (Visual Reasoning) Analytical Reasoning (Counting Figures Reasoning) Cubes & Dices Coding Decoding Ranking, Order and Sequence Series (Number and Letter Series) (Numerical Relations and Reasoning)
  3. Common Applications & MS Office (Technical Assessment): MS Office by Yash Sir
  4. Pseudo Code (Technical Assessment): Pseudo Codes
  5. Networking, Security & Cloud (Technical Assessment): 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 Emerging Tech Trends
  6. Python (Technical Assessment): Python for Placements by Yash Sir
  7. Java (Technical Assessment): Java
  8. OOPs (Technical Assessment): Object Oriented Programming (OOPS)
  9. Data Structure (Coding Round): Introduction to DS Array Stack Queue Linked List Tree Graphs Hashing
  10. Algorithms (Coding Round): Algorithm Analysis Sorting Algorithms Greedy Algorithms Dynamic Programming Minimum Spanning Trees Shortest Path Algos Time Complexity Analysis Advanced Algorithms
  11. Coding Assessment Questions: C Programming by Yash Sir Coding Questions by Yash Sir Programming in C++
  12. SQL (Coding Round): SQL
  13. JavaScript Frontend (Coding Round): JavaScript
  14. Communication Test (Basic Verbal Ability): Vocabulary Sentence Construction Noun Pronoun Verb Tenses Subject Verb Agreement (Verb Noun Agreement) Articles Preposition Conjunction Adverb Modals Sentence Correction (Error Correction) Sentence Completion (Fill in the blanks) Comprehension / Reading Comprehension / Unseen Passages (Critical Reasoning) (Paragraph Questions) Communication Assessment
  15. 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

  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…