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
- Communication Round: Vocabulary, Subject Verb Agreement (Verb Noun Agreement), Tenses, Comprehension / Reading Comprehension / Unseen Passages (Critical Reasoning) (Paragraph Questions), Cloze Test (Cloze Passage), Sentence Correction (Error Correction), Sentence Completion (Fill in the blanks), Sentence Re-arrangements (Para Jumbles) (Narrative Sequencing), Direct and Indirect Speech, Active Passive Voice (Voice), Verbal Analogy (Word Based Analogy), Articles, Preposition, Conjunction, Sentence Construction, English Writing Ability, Communication Assessment
- Quantitative Aptitude: Ratio and Proportion (Ratios), Divisibility Rules, Data Interpretation, Mixture and Alligation, Permutation and Combination, Logarithm, Number System, HCF LCM, Speed Time and Distance, Percentage, Average, Powers and Exponents (Surds and Indices), Time and Work, Mensuration and Geometry, Age Problems, Pipe and Cistern, Probability, Profit and Loss, Simple Interest and Compound Interest, Algebra
- Analytical and Logical Reasoning: Series (Number and Letter Series) (Numerical Relations and Reasoning), Coding Decoding, Data Sufficiency, Non Verbal Reasoning (Spatial Aptitude) (Spatial Reasoning) (Visual Reasoning), Directions (Direction Test), Ranking, Order and Sequence, Deductive and Inductive Reasoning (Logical Deduction and Induction) (Prepositional Reasoning), Selection Decision Table (Decision Making), Logical Sequence Of Words, Puzzle Solving (Puzzles), Syllogisms, Seating Arrangements, Calendar, Clock, Blood Relations, Cubes & Dices, Venn Diagram, Analytical Reasoning (Counting Figures Reasoning)
- Cognizant Gaming Aptitude: Gamified / Game Based Aptitude
- Java (Cluster 1): Java
- ANSI SQL MySQL (Cluster 1,2,3): SQL
- HTML, CSS, JavaScript (Cluster 1,3): HTML, CSS, JavaScript
- Python (Cluster 2): Python for Placements by Yash Sir
- Cloud Fundamentals (Cluster 2): Emerging Tech Trends
- C# (Cluster 3): C#
- OOPs: Object Oriented Programming (OOPS)
- Technical Assessment (Coding): Coding Questions by Yash Sir, C Programming by Yash Sir, Programming in C++, Pseudo Codes
- Data Structures: Introduction to DS, Array, Stack, Queue, Linked List, Tree, Graphs, Hashing
- DBMS: Basics of DBMS, ER Diagram, Relational Model & Functional Dependencies, Keys & Integrity Constraints, Normalization (1NF - BCNF), Decomposition Properties & 4NF, File Organization & Indexing, Relational Algebra, Relational Calculus, Transaction Management, Concurrency Control, Database Recovery, ORDBMS, Database Security & Authorization, Query Processing & Optimization, Enhanced Data Models, Data Warehousing & Mining, Big Data Systems, NoSQL
- Operating Systems: Introduction to OS, Process Management, CPU Scheduling, Process Synchronization, Deadlock, Threads & Process Creation, Memory Management, Virtual Memory, Disc Scheduling, File Management, Windows OS, Linux OS, Security, Distributed Systems, Virtual Machines
- Computer Networks: 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, Mobile Technology, Cloud Computing and IoT, Cyber and Info Security
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.