{"id":697,"date":"2025-07-21T13:50:47","date_gmt":"2025-07-21T08:20:47","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=697"},"modified":"2025-07-21T13:50:48","modified_gmt":"2025-07-21T08:20:48","slug":"bit-manipulation-introduction","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/","title":{"rendered":"Mastering Bit\u00a0Manipulation: Swap Two Numbers\u00a0Without Temporary\u00a0Variable and\u00a0Check if Kth Bit\u00a0is Set"},"content":{"rendered":"\n<p>Welcome to our detailed guide on bit manipulation problems! If you&#8217;re preparing for coding interviews or just want to strengthen your understanding of bitwise operations, you&#8217;re in the right place. In this article, we&#8217;ll dive deep into two classic problems: swapping two numbers without using a temporary variable and checking if the Kth bit is set in a number&#8217;s binary representation. We&#8217;ll explain everything in simple, plain English to make it easy for students to follow along.<\/p>\n\n\n\n<p>These problems are common in platforms like GeeksforGeeks and LeetCode, and they help build a strong foundation in data structures and algorithms. We&#8217;ll cover problem statements, intuitions, approaches, code examples, dry runs, edge cases, and time\/space complexities. By the end, you&#8217;ll feel confident tackling similar challenges.<\/p>\n\n\n\n<p>Let&#8217;s start with the first problem.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-870f1bb9-7cd5-4253-9689-076c44e4c4b7\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#0-swap-two-numbers-without-a-temporary-variable\" style=\"\">Swap Two Numbers Without a Temporary Variable<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#1-problem-statement\" style=\"\">Problem Statement<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#2-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#3-code-example-in-python\" style=\"\">Code Example in Python<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#4-dry-run-with-example\" style=\"\">Dry Run with Example<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#5-edge-cases\" style=\"\">Edge Cases<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#7-check-if-the-kth-bit-is-set-or-not\" style=\"\">Check if the Kth Bit is Set or Not<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#8-problem-statement\" style=\"\">Problem Statement<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#9-optimal-solution-1-using-left-shift-operator\" style=\"\">Optimal Solution 1: Using Left Shift Operator<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#10-code-example-in-python-left-shift\" style=\"\">Code Example in Python (Left Shift)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#11-dry-run-for-left-shift\" style=\"\">Dry Run for Left Shift<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#12-edge-cases-for-left-shift\" style=\"\">Edge Cases for Left Shift<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#13-time-and-space-complexity-left-shift\" style=\"\">Time and Space Complexity (Left Shift)<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#14-optimal-solution-2-using-right-shift-operator\" style=\"\">Optimal Solution 2: Using Right Shift Operator<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#15-code-example-in-python-right-shift\" style=\"\">Code Example in Python (Right Shift)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#16-dry-run-for-right-shift\" style=\"\">Dry Run for Right Shift<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#17-edge-cases-for-right-shift\" style=\"\">Edge Cases for Right Shift<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#18-time-and-space-complexity-right-shift\" style=\"\">Time and Space Complexity (Right Shift)<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#19-why-learn-these-bit-manipulation-techniques\" style=\"\">Why Learn These Bit Manipulation Techniques?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/bit-manipulation-introduction\/#20-references-and-further-reading\" style=\"\">References and Further Reading<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-swap-two-numbers-without-a-temporary-variable\">Swap Two Numbers Without a Temporary Variable<\/h2>\n\n\n\n<p>Swapping two numbers is a basic operation, but doing it without extra space (like a temporary variable) is a neat trick using bitwise XOR. This method is efficient and works in constant time, making it perfect for constrained environments.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-problem-statement\">Problem Statement<\/h3>\n\n\n\n<p><strong>Objective:<\/strong>&nbsp;Swap the values of two integers, a and b, without using any temporary variable.<\/p>\n\n\n\n<p><strong>Expected Input and Output:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\u00a0Two integers, a and b.<\/li>\n\n\n\n<li><strong>Output:<\/strong>\u00a0A list containing the swapped values [b, a].<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Example 1:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Input: a = 13, b = 9<\/li>\n\n\n\n<li>Output:<\/li>\n\n\n\n<li>Explanation: After swapping, a becomes 9 and b becomes 13.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Example 2:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Input: a = 15, b = 8<\/li>\n\n\n\n<li>Output:<\/li>\n\n\n\n<li>Explanation: After swapping, a becomes 8 and b becomes 15.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1 \u2264 a, b \u2264 10^6<\/li>\n<\/ul>\n\n\n\n<p><strong>Expected Time Complexity:<\/strong>&nbsp;O(1)<br><strong>Expected Auxiliary Space:<\/strong>&nbsp;O(1)<\/p>\n\n\n\n<p>This problem is straightforward but teaches us about bitwise operations&#8217; power. You don&#8217;t need to handle input\/output; just complete the function that returns the swapped list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p><strong>Intuition:<\/strong>&nbsp;We use the XOR (^) operator, a bitwise operation that flips bits. XOR has useful properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Any number XOR itself is 0 (e.g., x ^ x = 0).<\/li>\n\n\n\n<li>Any number XOR 0 is itself (e.g., x ^ 0 = x).<\/li>\n\n\n\n<li>XOR is associative and commutative, so x ^ y ^ x = y.<\/li>\n<\/ul>\n\n\n\n<p>These properties allow us to swap without losing data. Imagine XOR as a way to &#8220;encode&#8221; and &#8220;decode&#8221; values temporarily.<\/p>\n\n\n\n<p><strong>Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First, set a = a ^ b. Now, a holds the combined info of both numbers.<\/li>\n\n\n\n<li>Then, set b = a ^ b. This cancels out the original b and leaves b with the original a.<\/li>\n\n\n\n<li>Finally, set a = a ^ b. This cancels out the original a and leaves a with the original b.<\/li>\n\n\n\n<li>Return the list [a, b] with swapped values.<\/li>\n<\/ol>\n\n\n\n<p>This works because each step builds on the previous one, using XOR to toggle between values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-example-in-python\">Code Example in Python<\/h3>\n\n\n\n<p>Here&#8217;s the simple Python code for the solution:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def get(self, a, b):\n        a = a ^ b  # Step 1: a now holds XOR of original a and b\n        b = a ^ b  # Step 2: b now holds original a\n        a = a ^ b  # Step 3: a now holds original b\n        return [a, b]\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">get<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">a<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">b<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        a = a ^ b  <\/span><span style=\"color: #6A9955\"># Step 1: a now holds XOR of original a and b<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        b = a ^ b  <\/span><span style=\"color: #6A9955\"># Step 2: b now holds original a<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        a = a ^ b  <\/span><span style=\"color: #6A9955\"># Step 3: a now holds original b<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> [a, b]<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>First XOR:<\/strong>\u00a0a = a ^ b \u2192 a becomes a mix of a and b.<\/li>\n\n\n\n<li><strong>Second XOR:<\/strong>\u00a0b = a ^ b \u2192 Since a is now (original a ^ b), this becomes (original a ^ b) ^ b = original a (because b ^ b = 0).<\/li>\n\n\n\n<li><strong>Third XOR:<\/strong>\u00a0a = a ^ b \u2192 Now a is (original a ^ b) ^ original a = original b (because original a ^ original a = 0).<\/li>\n\n\n\n<li>Finally, return the swapped [a, b].<\/li>\n<\/ul>\n\n\n\n<p>This is elegant and uses no extra space.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-dry-run-with-example\">Dry Run with Example<\/h3>\n\n\n\n<p>Let&#8217;s simulate with a = 3, b = 5 (binary: 3 is 011, 5 is 101).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Initial: a = 3 (011), b = 5 (101)<\/li>\n\n\n\n<li>Step 1: a = 3 ^ 5 = 011 ^ 101 = 110 (6 in decimal)<\/li>\n\n\n\n<li>Step 2: b = 6 ^ 5 = 110 ^ 101 = 011 (3, original a)<\/li>\n\n\n\n<li>Step 3: a = 6 ^ 3 = 110 ^ 011 = 101 (5, original b)<\/li>\n\n\n\n<li>Return: [5,erfect swap! Try it with your own numbers to see.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-edge-cases\">Edge Cases<\/h3>\n\n\n\n<p><strong>Potential Edge Cases:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Identical Values:<\/strong>\u00a0a = 4, b = 4. XOR with same values gives 0, so it stays the same (which is correct for swap).<\/li>\n\n\n\n<li><strong>Zero Values:<\/strong>\u00a0a = 0, b = 5. Works fine: 0 ^ 5 = 5, then 5 ^ 5 = 0, then 5 ^ 0 = 5 \u2192 [5,\u00a0<strong>Maximum Values:<\/strong>\u00a0a = 10^6, b = 1. Handles large numbers since XOR is bitwise.<\/li>\n<\/ul>\n\n\n\n<p>The method is robust and doesn&#8217;t fail in these scenarios because of XOR&#8217;s properties.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(1) \u2013 Just three constant-time operations, no loops.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1) \u2013 No extra space used, only the input variables.<\/li>\n<\/ul>\n\n\n\n<p>This makes it optimal for interviews where efficiency matters.<\/p>\n\n\n\n<p>Now, let&#8217;s move to the second problem, which also uses bit manipulation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-check-if-the-kth-bit-is-set-or-not\">Check if the Kth Bit is Set or Not<\/h2>\n\n\n\n<p>This problem tests your ability to inspect individual bits in a number&#8217;s binary form. It&#8217;s useful in scenarios like flag checking or low-level programming.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-problem-statement\">Problem Statement<\/h3>\n\n\n\n<p><strong>Objective:<\/strong>&nbsp;Determine if the Kth bit (0-based index from the right) in the binary representation of n is set (1) or not (0).<\/p>\n\n\n\n<p><strong>Expected Input and Output:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\u00a0Integer n and integer k (bit position).<\/li>\n\n\n\n<li><strong>Output:<\/strong>\u00a0&#8220;Yes&#8221; if set, &#8220;No&#8221; if not (but in code, we return True\/False).<\/li>\n<\/ul>\n\n\n\n<p><strong>Examples:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong>\u00a0n = 4 (binary 100), k = 0 \u2192 Output: No (0th bit is 0)<\/li>\n\n\n\n<li><strong>Input:<\/strong>\u00a0n = 4 (binary 100), k = 2 \u2192 Output: Yes (2nd bit is 1)<\/li>\n\n\n\n<li><strong>Input:<\/strong>\u00a0n = 500 (binary 111110100), k = 3 \u2192 Output: No (3rd bit is 0)<\/li>\n<\/ul>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1 \u2264 n \u2264 10^9<\/li>\n\n\n\n<li>0 \u2264 k \u2264 31<\/li>\n<\/ul>\n\n\n\n<p><strong>Expected Time Complexity:<\/strong>&nbsp;O(1)<br><strong>Expected Auxiliary Space:<\/strong>&nbsp;O(1)<\/p>\n\n\n\n<p>Note: Bits are counted from the least significant bit (rightmost) as 0.<\/p>\n\n\n\n<p>We&#8217;ll cover two optimal ways: using left shift and right shift. Both are efficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-optimal-solution-1-using-left-shift-operator\">Optimal Solution 1: Using Left Shift Operator<\/h2>\n\n\n\n<p><strong>Intuition:<\/strong>&nbsp;Create a &#8220;mask&#8221; with only the Kth bit set to 1, then AND it with n. If the result isn&#8217;t zero, the bit is set.<\/p>\n\n\n\n<p><strong>Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Shift 1 left by k positions: mask = 1 &lt;&lt; k (e.g., for k=2, 1 becomes 100).<\/li>\n\n\n\n<li>Do n &amp; mask. If >0, bit is set.<\/li>\n\n\n\n<li>Return True or False.<\/li>\n<\/ol>\n\n\n\n<p>This isolates the exact bit we care about.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-code-example-in-python-left-shift\">Code Example in Python (Left Shift)<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def checkKthBit(self, n, k):\n        if n &amp; (1 &lt;&lt; k) != 0:\n            return True\n        return False\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">checkKthBit<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">k<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> n &amp; (<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> &lt;&lt; k) != <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1 &lt;&lt; k creates a number with only the Kth bit as 1.<\/li>\n\n\n\n<li>n &amp; that number keeps only the Kth bit from n.<\/li>\n\n\n\n<li>If it&#8217;s not zero, the bit was 1; else, 0.<\/li>\n<\/ul>\n\n\n\n<p>Simple and direct.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-dry-run-for-left-shift\">Dry Run for Left Shift<\/h3>\n\n\n\n<p>Input: n=5 (101), k=2<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Mask: 1 &lt;&lt; 2 = 100 (4)<\/li>\n\n\n\n<li>5 &amp; 4 = 100 (4, non-zero) \u2192 True (bit is set)<\/li>\n<\/ul>\n\n\n\n<p>Another: n=4 (100), k=0<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Mask: 1 &lt;&lt; 0 = 1 (001)<\/li>\n\n\n\n<li>4 &amp; 1 = 0 \u2192 False<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-edge-cases-for-left-shift\">Edge Cases for Left Shift<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>k=0:<\/strong>\u00a0Checks LSB correctly.<\/li>\n\n\n\n<li><strong>k > bits in n:<\/strong>\u00a0E.g., n=5 (3 bits), k=3 \u2192 1&lt;&lt;3=8, 5&amp;8=0 \u2192 False (correct, as higher bits are 0).<\/li>\n\n\n\n<li><strong>n=0:<\/strong>\u00a0Any k \u2192 False.<\/li>\n<\/ul>\n\n\n\n<p>Handles all nicely.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-time-and-space-complexity-left-shift\">Time and Space Complexity (Left Shift)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong>\u00a0O(1) \u2013 Constant operations.<\/li>\n\n\n\n<li><strong>Space:<\/strong>\u00a0O(1) \u2013 No extra space.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-optimal-solution-2-using-right-shift-operator\">Optimal Solution 2: Using Right Shift Operator<\/h2>\n\n\n\n<p><strong>Intuition:<\/strong>&nbsp;Move the Kth bit to the rightmost position, then check if it&#8217;s 1.<\/p>\n\n\n\n<p><strong>Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Shift n right by k: n >> k (Kth bit now at position 0).<\/li>\n\n\n\n<li>AND with 1: (n >> k) &amp; 1. If 1, bit was set.<\/li>\n\n\n\n<li>Return True or False.<\/li>\n<\/ol>\n\n\n\n<p>This &#8220;slides&#8221; the bit into an easy-to-check spot.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-code-example-in-python-right-shift\">Code Example in Python (Right Shift)<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def checkKthBit(self, n, k):\n        if (n &gt;&gt; k) &amp; 1 == 1:\n            return True\n        return False\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">checkKthBit<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">k<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> (n &gt;&gt; k) &amp; <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> == <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>n >> k moves bits right, putting Kth at LSB.<\/li>\n\n\n\n<li>&amp; 1 extracts that bit (1 if set, 0 if not).<\/li>\n<\/ul>\n\n\n\n<p>Equally efficient.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-dry-run-for-right-shift\">Dry Run for Right Shift<\/h3>\n\n\n\n<p>Input: n=5 (101), k=2<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>5 >> 2 = 1 (001)<\/li>\n\n\n\n<li>1 &amp; 1 = 1 \u2192 True<\/li>\n<\/ul>\n\n\n\n<p>Another: n=4 (100), k=0<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>4 >> 0 = 4 (100)<\/li>\n\n\n\n<li>4 &amp; 1 = 0 (since LSB is 0) \u2192 False<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"17-edge-cases-for-right-shift\">Edge Cases for Right Shift<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>k=0:<\/strong>\u00a0Directly checks LSB.<\/li>\n\n\n\n<li><strong>k > bits in n:<\/strong>\u00a0Shifts to 0, &amp;1=0 \u2192 False.<\/li>\n\n\n\n<li><strong>n=0:<\/strong>\u00a0Always False.<\/li>\n<\/ul>\n\n\n\n<p>Robust like the left shift method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"18-time-and-space-complexity-right-shift\">Time and Space Complexity (Right Shift)<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong>\u00a0O(1) \u2013 Constant.<\/li>\n\n\n\n<li><strong>Space:<\/strong>\u00a0O(1) \u2013 Minimal.<\/li>\n<\/ul>\n\n\n\n<p>Both methods are great; choose based on preference. Left shift might feel like &#8220;masking,&#8221; right shift like &#8220;extracting.&#8221;<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"19-why-learn-these-bit-manipulation-techniques\">Why Learn These Bit Manipulation Techniques?<\/h2>\n\n\n\n<p>Bit manipulation is crucial for optimizing code in terms of speed and space, especially in embedded systems or competitive programming. Problems like these appear in interviews at companies like Google, Amazon, and Microsoft. Practicing them builds intuition for more complex topics like bitmasks or dynamic programming with bits.<\/p>\n\n\n\n<p>If you&#8217;re new, try implementing these in your favorite language and test with different inputs. For more practice, check GeeksforGeeks links provided in the references.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"20-references-and-further-reading\">References and Further Reading<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Swap Two Numbers:\u00a0<a href=\"https:\/\/www.geeksforgeeks.org\/problems\/swap-two-numbers3844\/1\" target=\"_blank\" rel=\"noreferrer noopener\">GeeksforGeeks Problem<\/a><\/li>\n\n\n\n<li>Check Kth Bit:\u00a0<a href=\"https:\/\/www.geeksforgeeks.org\/problems\/check-whether-k-th-bit-is-set-or-not-1587115620\/1\" target=\"_blank\" rel=\"noreferrer noopener\">GeeksforGeeks Problem<\/a><\/li>\n<\/ul>\n\n\n\n<p>Feel free to comment below if you have questions or share your own solutions! Stay tuned for more coding tutorials.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to our detailed guide on bit manipulation problems! If you&#8217;re preparing for coding interviews or just want to strengthen your understanding of bitwise operations, you&#8217;re in the right place. In this article, we&#8217;ll dive deep into two classic problems: swapping two numbers without using a temporary variable and checking if the Kth bit is<\/p>\n","protected":false},"author":1,"featured_media":699,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[31],"class_list":{"0":"post-697","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-bit-manipulation"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/bit-manipulation-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=697"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":700,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/697\/revisions\/700"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/699"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}