{"id":3123,"date":"2026-11-10T11:50:03","date_gmt":"2026-11-10T11:50:03","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3123"},"modified":"2026-11-10T11:50:03","modified_gmt":"2026-11-10T11:50:03","slug":"python-bitwise-operators","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-bitwise-operators\/","title":{"rendered":"Python Bitwise Operators with Examples"},"content":{"rendered":"<p>Bitwise operators work directly on the binary digits (bits) of whole numbers. You will not need them in every program, but they show up in interviews, competitive programming, permissions and flags, low-level hardware code and some quick tricks such as checking whether a number is even.<\/p>\n<p>To follow this lesson, it helps to see numbers in binary. Python&#8217;s <code>bin()<\/code> function does that: <code>bin(12)<\/code> is <code>'0b1100'<\/code>, where <code>0b<\/code> just marks a binary number.<\/p>\n<h2>Python Bitwise Operators Table<\/h2>\n<table>\n<thead>\n<tr>\n<th>Operator<\/th>\n<th>Name<\/th>\n<th>What it does<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>&amp;<\/code><\/td>\n<td>AND<\/td>\n<td>A bit is 1 only if it is 1 in <strong>both<\/strong> numbers<\/td>\n<\/tr>\n<tr>\n<td><code>|<\/code><\/td>\n<td>OR<\/td>\n<td>A bit is 1 if it is 1 in <strong>either<\/strong> number<\/td>\n<\/tr>\n<tr>\n<td><code>^<\/code><\/td>\n<td>XOR<\/td>\n<td>A bit is 1 if the two bits are <strong>different<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>~<\/code><\/td>\n<td>NOT<\/td>\n<td>Flips every bit; in Python <code>~n<\/code> equals <code>-(n + 1)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&lt;&lt;<\/code><\/td>\n<td>Left shift<\/td>\n<td>Moves bits left; <code>n &lt;&lt; k<\/code> equals <code>n \u00d7 2<sup>k<\/sup><\/code><\/td>\n<\/tr>\n<tr>\n<td><code>&gt;&gt;<\/code><\/td>\n<td>Right shift<\/td>\n<td>Moves bits right; <code>n &gt;&gt; k<\/code> equals <code>n \/\/ 2<sup>k<\/sup><\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Example with 12 and 10<\/h2>\n<pre class=\"wp-block-code\"><code>a = 12\nb = 10\nprint(bin(a), bin(b))\nprint(a &amp; b, bin(a &amp; b))\nprint(a | b, bin(a | b))\nprint(a ^ b, bin(a ^ b))\nprint(~a)\nprint(a &lt;&lt; 2)\nprint(a &gt;&gt; 2)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>0b1100 0b1010\n8 0b1000\n14 0b1110\n6 0b110\n-13\n48\n3<\/code><\/pre>\n<p>Here is the working, bit by bit:<\/p>\n<pre class=\"wp-block-code\"><code>  12 = 1 1 0 0\n  10 = 1 0 1 0\n-----------------\nAND  = 1 0 0 0  = 8\nOR   = 1 1 1 0  = 14\nXOR  = 0 1 1 0  = 6<\/code><\/pre>\n<p><code>12 &lt;&lt; 2<\/code> shifts <code>1100<\/code> two places left to get <code>110000<\/code>, which is 48 (12 \u00d7 4). <code>12 &gt;&gt; 2<\/code> shifts it two places right to get <code>11<\/code>, which is 3 (12 \/\/ 4).<\/p>\n<h2>Why Is ~12 Equal to -13?<\/h2>\n<p>Python integers have no fixed size, and negative numbers behave as if they were stored in &#8220;two&#8217;s complement&#8221; form. In that system, flipping every bit of <code>n<\/code> always gives <code>-(n + 1)<\/code>. So <code>~12<\/code> is <code>-13<\/code>, <code>~0<\/code> is <code>-1<\/code>, and <code>~-1<\/code> is <code>0<\/code>. You don&#8217;t need to work through the binary every time; just remember the formula.<\/p>\n<h2>Shifts as Fast Multiplication and Division<\/h2>\n<pre class=\"wp-block-code\"><code>print(5 &lt;&lt; 1, 5 &lt;&lt; 3, 40 &gt;&gt; 1, 40 &gt;&gt; 3)\nprint(-9 &gt;&gt; 1, -9 \/\/ 2)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>10 40 20 5\n-5 -5<\/code><\/pre>\n<p>Left shift by <code>k<\/code> multiplies by 2<sup>k<\/sup>, and right shift by <code>k<\/code> floor-divides by 2<sup>k<\/sup>. For negative numbers the right shift rounds down (towards minus infinity), just like <code>\/\/<\/code>.<\/p>\n<h2>Practical Uses<\/h2>\n<h3>1. Check Even or Odd<\/h3>\n<p>The last bit of an odd number is always 1, so <code>n &amp; 1<\/code> tells you whether a number is odd:<\/p>\n<pre class=\"wp-block-code\"><code>for n in (7, 10):\n    print(n, \"odd\" if n &amp; 1 else \"even\")<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>7 odd\n10 even<\/code><\/pre>\n<p>In everyday Python code, <code>n % 2 == 0<\/code> is clearer and just as fast, but the bitwise version is common in interview questions.<\/p>\n<h3>2. Check if a Number Is a Power of Two<\/h3>\n<pre class=\"wp-block-code\"><code>for n in (8, 12, 64):\n    print(n, n &gt; 0 and (n &amp; (n - 1)) == 0)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>8 True\n12 False\n64 True<\/code><\/pre>\n<p>A power of two has exactly one 1-bit (8 is <code>1000<\/code>). Subtracting 1 turns that bit off and all the bits below it on (7 is <code>0111<\/code>), so ANDing them gives 0.<\/p>\n<h3>3. Permissions with Flags<\/h3>\n<p>Many systems, including Linux file permissions, store several yes\/no settings in one number. Each setting gets its own bit:<\/p>\n<pre class=\"wp-block-code\"><code>READ = 4      # 100\nWRITE = 2     # 010\nEXECUTE = 1   # 001\n\nperm = READ | WRITE        # give read and write\nprint(perm)\nprint(bool(perm &amp; WRITE))  # has write?\nprint(bool(perm &amp; EXECUTE))\n\nperm = perm | EXECUTE      # add execute\nprint(perm)\nperm = perm &amp; ~WRITE       # remove write\nprint(perm)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>6\nTrue\nFalse\n7\n5<\/code><\/pre>\n<p>This is exactly why a Linux permission like <code>7<\/code> means read + write + execute, and <code>5<\/code> means read + execute.<\/p>\n<h3>4. Swap Two Numbers with XOR<\/h3>\n<pre class=\"wp-block-code\"><code>x, y = 5, 9\nx = x ^ y\ny = x ^ y\nx = x ^ y\nprint(x, y)<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>9 5<\/code><\/pre>\n<p>This is a classic interview trick. In real Python code, simply write <code>x, y = y, x<\/code>.<\/p>\n<h2>Converting Between Binary and Decimal<\/h2>\n<pre class=\"wp-block-code\"><code>print(format(12, \"08b\"))\nprint(int(\"1100\", 2))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>00001100\n12<\/code><\/pre>\n<p><code>format(n, \"08b\")<\/code> shows a number as 8 binary digits with leading zeros, and <code>int(text, 2)<\/code> reads a binary string back into a number.<\/p>\n<h2>Bitwise vs Logical Operators<\/h2>\n<p><code>&amp;<\/code> and <code>|<\/code> are not the same as <code>and<\/code> and <code>or<\/code>. Bitwise operators always evaluate both sides and work bit by bit, while <a href=\"https:\/\/gyanipandit.com\/programming\/python-logical-operators\/\">logical operators<\/a> stop early (short-circuit). With plain True\/False values they give the same answer, but be careful with brackets, because <code>&amp;<\/code> binds more tightly than comparisons:<\/p>\n<pre class=\"wp-block-code\"><code>print(True &amp; False, True | False)\nprint((5 &gt; 3) &amp; (2 &gt; 1))<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>False True\nTrue<\/code><\/pre>\n<p>Without the brackets, <code>5 &gt; 3 &amp; 2 &gt; 1<\/code> would be read as <code>5 &gt; (3 &amp; 2) &gt; 1<\/code>. For conditions, use <code>and<\/code> \/ <code>or<\/code>.<\/p>\n<h2>Practice Questions<\/h2>\n<ol>\n<li><code>print(9 &amp; 5, 9 | 5, 9 ^ 5)<\/code><\/li>\n<li><code>print(1 &lt;&lt; 10)<\/code><\/li>\n<li><code>print(~0)<\/code><\/li>\n<\/ol>\n<p><strong>Answers:<\/strong> 1) <code>1 13 12<\/code>, because 9 is <code>1001<\/code> and 5 is <code>0101<\/code>. 2) <code>1024<\/code>, which is 2<sup>10<\/sup>. 3) <code>-1<\/code>, because <code>~n<\/code> is <code>-(n + 1)<\/code>.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What are bitwise operators in Python?<\/h3>\n<p>Operators that work on the individual bits of integers: <code>&amp;<\/code> (AND), <code>|<\/code> (OR), <code>^<\/code> (XOR), <code>~<\/code> (NOT), <code>&lt;&lt;<\/code> (left shift) and <code>&gt;&gt;<\/code> (right shift).<\/p>\n<h3>Why does ~5 give -6 in Python?<\/h3>\n<p>Because Python follows two&#8217;s complement rules for negative numbers, so <code>~n<\/code> always equals <code>-(n + 1)<\/code>.<\/p>\n<h3>What is the difference between &amp; and and?<\/h3>\n<p><code>&amp;<\/code> compares numbers bit by bit and always evaluates both sides. <code>and<\/code> works on truth values and stops as soon as the result is known.<\/p>\n<h3>Where are bitwise operators used in real life?<\/h3>\n<p>In permission flags, network addresses and subnet masks, graphics and colour values, compression, encryption and hardware programming.<\/p>\n<p>Next: <a href=\"https:\/\/gyanipandit.com\/programming\/membership-operators-in-python\/\">Membership operators in Python<\/a> \u00b7 Previous: <a href=\"https:\/\/gyanipandit.com\/programming\/python-comparison-operators\/\">Python comparison operators<\/a> \u00b7 All operators: <a href=\"https:\/\/gyanipandit.com\/programming\/python-operators\/\">Python operators overview<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bitwise operators work directly on the binary digits (bits) of whole numbers. You will not need them in every program, but they show up in interviews, competitive programming, permissions and flags, low-level hardware code and some quick tricks such as checking whether a number is even. To follow this lesson, it helps to see numbers &#8230; <a title=\"Python Bitwise Operators with Examples\" class=\"read-more\" href=\"https:\/\/gyanipandit.com\/programming\/python-bitwise-operators\/\" aria-label=\"Read more about Python Bitwise Operators with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":["post-3123","post","type-post","status-publish","format-standard","hentry","category-python-tutorial","tag-python","tag-python-tutorial"],"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3123"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3123\/revisions"}],"predecessor-version":[{"id":6129,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3123\/revisions\/6129"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}