{"id":6985,"date":"2024-04-11T12:01:03","date_gmt":"2024-04-11T12:01:03","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=6985"},"modified":"2024-04-11T12:01:03","modified_gmt":"2024-04-11T12:01:03","slug":"python-bitwise-operator","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-bitwise-operator\/","title":{"rendered":"Python Bitwise Operator"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-python-bitwise-operator\">What is Python Bitwise Operator<\/a><\/li><li ><a href=\"#what-is-bitwise-and-operator\">What is Bitwise  And Operator?<\/a><\/li><li ><a href=\"#what-is-bitwise-or-operator\">What is Bitwise  OR Operator?<\/a><\/li><li ><a href=\"#what-is-bitwise-not-operator\">What is Bitwise NOT operator?<\/a><\/li><li ><a href=\"#what-is-bitwise-xor-operator\">What is Bitwise XOR Operator?<\/a><\/li><li ><a href=\"#what-is-a-shift-operator\">What is a Shift Operator?<\/a><\/li><li ><a href=\"#what-is-bitwise-operator-overloading\">What is Bitwise Operator Overloading?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-bitwise-operator-fa-qs\">Python Bitwise Operator- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>Python Bitwise operators are mostly used with integer-type objects.  Hence, Bitwise operation will be executed through bitwise calculations on integers.  Read this article to learn more about the Python Bitwise operator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-bitwise-operator\">What is Python Bitwise Operator<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/python-syntax\/\" data-type=\"post\" data-id=\"6924\">Python<\/a> Bitwise<a href=\"https:\/\/www.skillvertex.com\/blog\/python-operators\/\" data-type=\"post\" data-id=\"6937\"> operators<\/a> will work with integers. Whereas, they will consider the object as a string instead of treating it as a whole. Thus, different operations will be done on each bit in a string.<\/p>\n\n\n\n<p>Moreover, Python has six bitwise operators such as  &amp;, |, ^, ~, &lt;&lt; and &gt;&gt;. Each of these operators is mostly binary. This means they can work on the two <strong>operands<\/strong>. Each operand is referred to as a binary digit(bit)  ie, 1 and 0.<\/p>\n\n\n\n<p>Let us take a look at the table of the different operators of  Bitwise<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-bitwise-and-operator\">What is Bitwise  And Operator?<\/h2>\n\n\n\n<p>The Bitwise <a href=\"https:\/\/www.skillvertex.com\/blog\/and-operators-in-c\/\" data-type=\"post\" data-id=\"3294\">AND operator<\/a> works similarly to the <strong>logical AND operator.  This will provide the result <\/strong>as true, if both the operands will come as 1. The combinations are given below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 &amp; 0 is 0\n1 &amp; 0 is 0\n0 &amp; 1 is 0\n1 &amp; 1 is 1<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><em>OPERATOR<\/em><\/th><th><em>NAME<\/em><\/th><th><em>DESCRIPTION<\/em><\/th><th><em>SYNTAX<\/em><\/th><\/tr><\/thead><tbody><tr><td>&amp;<\/td><td>Bitwise AND<\/td><td>Bit wise operator has result bit 1, if both operand bits are 1; or results bit 0.<\/td><td>x &amp; y<\/td><\/tr><tr><td>~<\/td><td>Bitwise NOT<\/td><td>Bitwise NOT operator will invert the individual bits<\/td><td>~x<\/td><\/tr><tr><td>|<\/td><td>Bitwise Or<\/td><td>This operator will give the result bit as 1 only if the operand bit is 1 or it will give 0<\/td><td>x | y<br><br><\/td><\/tr><tr><td>^<\/td><td>Bitwise XOR<\/td><td>This operator will provide results bit 1 only if any of the operand bit is 1 , or else it will result bit in  0.<\/td><td>x ^ y<\/td><\/tr><tr><td>&lt;&lt;<\/td><td>Bitwise Left shift<\/td><td>The left operand value will be moved toward&#8217;s the right by checking the number of bits.<\/td><td>x&lt;&lt;<\/td><\/tr><tr><td>&gt;&gt;<\/td><td>Bitwise Right  shift<\/td><td>The left operand value will be moved toward&#8217;s the right by checking the number of bits.<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10 = 1010 (Binary)\nb = 4 =  0100 (Binary)\n\na &amp; b = 1010\n         &amp;\n        0100\n      = 0000\n      = 0 (Decimal)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-bitwise-or-operator\">What is Bitwise  OR Operator?<\/h2>\n\n\n\n<p>This symbol &#8220;<strong>|<\/strong>&#8221;&nbsp;  is referred to as Pipe and is also known as <a href=\"https:\/\/www.skillvertex.com\/blog\/python-logical-operators\/\" data-type=\"post\" data-id=\"6978\">Bitwise OR Operator<\/a>. Only if the  bit operand is 1, then it will result in 1, or else it is 0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 | 0 is 0\n0 | 1 is 1\n1 | 0 is 1\n1 | 1 is 1<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10 = 1010 (Binary)\nb = 4 =  0100 (Binary)\n\na | b = 1010\n         |\n        0100\n      = 1110\n      = 14 (Decimal)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-bitwise-not-operator\">What is Bitwise NOT operator?<\/h2>\n\n\n\n<p>This <strong>bitwise operator<\/strong> is referred to as the <a href=\"https:\/\/www.skillvertex.com\/blog\/difference-between-binary-search-and-linear-search\/\" data-type=\"post\" data-id=\"520\">binary e<\/a>quivalent of logical <a href=\"https:\/\/www.skillvertex.com\/blog\/operators-in-c-set-2-relational-and-logical-operators\/\" data-type=\"post\" data-id=\"2202\">NOT operators<\/a>. It will flip each bit, so, 1 will be replaced by 0 and 0 by 1. Hence, it will return the complement of the original number.<\/p>\n\n\n\n<p>Moreover, python will use 2&#8217;s complement method. For example, <a href=\"https:\/\/www.skillvertex.com\/blog\/integer-promotions-in-c\/\" data-type=\"post\" data-id=\"1968\">positive integers <\/a>can be achieved by reversing the<a href=\"https:\/\/www.skillvertex.com\/blog\/bitwise-operators-in-c-c\/\" data-type=\"post\" data-id=\"2037\"> bits<\/a>.  In contrast, negative numbers, -x  will be written with the<a href=\"https:\/\/www.skillvertex.com\/blog\/bit-fields-in-c\/\" data-type=\"post\" data-id=\"2822\"> bit<\/a> pattern for (x-1)  along all the bits complemented ( switched from 1 to 0  or 0  to 1).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-1 is complement(1 - 1) = complement(0) = \"11111111\"\n-10 is complement(10 - 1) = complement(9) = complement(\"00001001\") = \"11110110\".<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10 = 1010 (Binary)\n\nIn computers we usually represent numbers using 32 bits,\nso binary representation of 10 is (....0000 1010)&#91;32 bits]\n\n~a is basically 1's complement of a \ni.e ~a should be ~10 = ~(....0000 1010) = (....1111 0101) = intermediate-result\n\nSince bitwise negation inverts the sign bit,\nwe now have a negative number. And we represent a negative number\nusing 2's complement.\n\n2's complement of intermediate-result is:\nintermediate-res =  0101      \/\/....1111 0101\n      \n                     1010      \/\/....0000 1010 -(1's complement)\n\n                         +1    \n                 -----------\n                   =  1011      \/\/....0000 1011\n                  -----------\n                   =   -11 (Decimal)\n                   \nthus ~a = -11<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-bitwise-xor-operator\">What is Bitwise XOR Operator?<\/h2>\n\n\n\n<p>The full form of<strong> XOR<\/strong> is the exclusive <strong>OR<\/strong>. So, the result that is provided by the OR operator on two bits is 1, or else one of the bits is 1. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 ^ 0 is 0\n0 ^ 1 is 1\n1 ^ 0 is 1\n1 ^ 1 is 0<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 10 = 1010 (Binary)\nb = 4 =  0100 (Binary)\n\na ^ b = 1010\n         ^\n        0100\n      = 1110\n      = 14 (Decimal)<\/code><\/pre>\n\n\n\n<p>Python Program on the bitwise operator will be given below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to show\n# bitwise operators\n \na = 10\nb = 4\n \n# Print bitwise AND operation\nprint(\"a &amp; b =\", a &amp; b)\n \n# Print bitwise OR operation\nprint(\"a | b =\", a | b)\n \n# Print bitwise NOT operation\nprint(\"~a =\", ~a)\n \n# print bitwise XOR operation\nprint(\"a ^ b =\", a ^ b)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a &amp; b = 0\na | b = 14\n~a = -11\na ^ b = 14<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-shift-operator\">What is a Shift Operator?<\/h2>\n\n\n\n<p>This<strong> Shift operator<\/strong> functions to shift the bits of a number left or right and then multiply or divide the number by two respectively. This operator is mainly used to multiply or divide a number by two.<\/p>\n\n\n\n<p><strong>Bitwise right shift<\/strong><\/p>\n\n\n\n<p>This operator is used to shift the bits of the number to the right and will fill 0 on the voids left as the result. Therefore, a similar effect can be achieved by dividing the number with some power of two.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Example 1:\na = 10 = 0000 1010 (Binary)\na &gt;&gt; 1 = 0000 0101 = 5\n\nExample 2:\na = -10 = 1111 0110 (Binary)\na &gt;&gt; 1 = 1111 1011 = -5 <\/code><\/pre>\n\n\n\n<p><strong>Bitwise left shift<\/strong><\/p>\n\n\n\n<p>This operator will be used to shift the bits to the number to the left and that will fill 0 on voids right as the end result.  Whereas, a similar effect can be achieved through multiplying the number with some power of two.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Example 1:\na = 5 = 0000 0101 (Binary)\na &lt;&lt; 1 = 0000 1010 = 10\na &lt;&lt; 2 = 0001 0100 = 20 \n\nExample 2:\nb = -10 = 1111 0110 (Binary)\nb &lt;&lt; 1 = 1110 1100 = -20\nb &lt;&lt; 2 = 1101 1000 = -40 <\/code><\/pre>\n\n\n\n<p>Python program is illustrated below to show the shift operators.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to show\n# shift operators\n \na = 10\nb = -10\n \n# print bitwise right shift operator\nprint(\"a &gt;&gt; 1 =\", a &gt;&gt; 1)\nprint(\"b &gt;&gt; 1 =\", b &gt;&gt; 1)\n \na = 5\nb = -10\n \n# print bitwise left shift operator\nprint(\"a &lt;&lt; 1 =\", a &lt;&lt; 1)\nprint(\"b &lt;&lt; 1 =\", b &lt;&lt; 1)<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a &gt;&gt; 1 = 5\nb &gt;&gt; 1 = -5\na &lt;&lt; 1 = 10\nb &lt;&lt; 1 = -20<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-bitwise-operator-overloading\">What is Bitwise Operator Overloading?<\/h2>\n\n\n\n<p><strong>Bitwise Operator Overloading<\/strong> is referred to as the extended meaning that is beyond the predefined operational meaning.  For instance, operator <br>+   mostly functions to add the two integers and even join the two strings and combine the two lists. <\/p>\n\n\n\n<p>Moreover, this can be gained a the +  will be overloaded with the int class and str class. Operator Overloading consists of the same built-in operator or can work to demonstrate the different behavior of objects in the different classes.<\/p>\n\n\n\n<p>An example to show the Bitwise Operator Overloading is  given  below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to demonstrate\n# operator overloading\n \n \nclass Geek():\n    def __init__(self, value):\n        self.value = value\n \n    def __and__(self, obj):\n        print(\"And operator overloaded\")\n        if isinstance(obj, Geek):\n            return self.value &amp; obj.value\n        else:\n            raise ValueError(\"Must be a object of class  skillvertex\")\n \n    def __or__(self, obj):\n        print(\"Or operator overloaded\")\n        if isinstance(obj, Geek):\n            return self.value | obj.value\n        else:\n            raise ValueError(\"Must be a object of class  skillvertex\")\n \n    def __xor__(self, obj):\n        print(\"Xor operator overloaded\")\n        if isinstance(obj, Geek):\n            return self.value ^ obj.value\n        else:\n            raise ValueError(\"Must be a object of class Skilvertex\")\n \n    def __lshift__(self, obj):\n        print(\"lshift operator overloaded\")\n        if isinstance(obj, Geek):\n            return self.value &lt;&lt; obj.value\n        else:\n            raise ValueError(\"Must be a object of class skillvertex\")\n \n    def __rshift__(self, obj):\n        print(\"rshift operator overloaded\")\n        if isinstance(obj, Geek):\n            return self.value &gt;&gt; obj.value\n        else:\n            raise ValueError(\"Must be a object of class  skill vertex\")\n \n    def __invert__(self):\n        print(\"Invert operator overloaded\")\n        return ~self.value\n \n \n# Driver's code\nif __name__ == \"__main__\":\n    a =  skillvertex(10)\n    b =  skillvertex(12)\n    print(a &amp; b)\n    print(a | b)\n    print(a ^ b)\n    print(a &lt;&lt; b)\n    print(a &gt;&gt; b)\n    print(~a)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>And operator overloaded\n8\nOr operator overloaded\n14\nXor operator overloaded\n8\nlshift operator overloaded\n40960\nrshift operator overloaded\n8\nInvert operator overloaded\n-11<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>To conclude, this article will improve your knowledge of the different types of Python Bitwise operators. Those are Bitwise AND operator, Bitwise OR operator, Bitwise NOT operator, and several others are included.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-bitwise-operator-fa-qs\">Python Bitwise Operator- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1707461409899\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is a bitwise operator in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The bitwise operator will function to do the bitwise calculations on integers.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707461420044\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is == and != in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. These are used to compare the value of the two objects. <\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707461430593\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is a tuple in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  It is a data structure type that works similarly to the list.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Bitwise operators are mostly used with integer-type objects. Hence, Bitwise operation will be executed through bitwise calculations on integers. Read this article to learn more about the Python Bitwise operator. What is Python Bitwise Operator Python Bitwise operators will work with integers. Whereas, they will consider the object as a string instead of treating &#8230; <a title=\"Python Bitwise Operator\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-bitwise-operator\/\" aria-label=\"More on Python Bitwise Operator\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":6987,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[57,894,892],"class_list":["post-6985","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python","tag-python-bitwise-operator","tag-python-operator","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=6985"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6985\/revisions"}],"predecessor-version":[{"id":8904,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6985\/revisions\/8904"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/6987"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=6985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=6985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=6985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}