{"id":13577,"date":"2021-03-31T16:46:00","date_gmt":"2021-03-31T16:46:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=13577"},"modified":"2021-04-12T16:46:54","modified_gmt":"2021-04-12T16:46:54","slug":"numpy-bitwise-operations","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-bitwise-operations","title":{"rendered":"5 NumPy Bitwise Operations to know!"},"content":{"rendered":"\n<p>Hello, readers! In this article, we will be focusing on <strong>5 NumPy Bitwise Operations<\/strong> that we should know!<\/p>\n\n\n\n<p>So, let us get started!<\/p>\n\n\n\n<p>To begin with, Bitwise <a href=\"https:\/\/www.askpython.com\/python\/python-operators\" target=\"_blank\" aria-label=\"operators (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">operators<\/a> help us to perform bit level operations i.e. bit by bit operations through a layer of abstraction enclosed within functions.<\/p>\n\n\n\n<p>In the course of the topic, we would be covering the below topics as a part of this article&#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>AND operation<\/strong><\/li><li><strong>OR operation<\/strong><\/li><li><strong>XOR operation<\/strong><\/li><li><strong>Invert operation<\/strong><\/li><li><strong>Integer to Binary representation<\/strong><\/li><\/ol>\n\n\n\n<p>Let us begin! \ud83d\ude42<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. NumPy Bitwise Operations &#8211; AND<\/h2>\n\n\n\n<p>The <strong>NumPy Bitwise AND operator<\/strong> enables us to perform bitwise AND operation on the array like input values. That is, it performs AND operation on the binary representation of the input integer values altogether.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.bitwise_and(num1,num2)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>In the below example, the bitwise_and() function converts the integer values 2 and 3 to their equivalent binary values i.e. 2 ~ 010 and 3 ~ 011. Further, it performs the AND operation which returns 1 as the resultant bit if both the equivalent bits are 1, else it returns 0.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nx = 2\ny = 3\n \nprint (&quot;x=&quot;,x)\nprint (&quot;y=&quot;,y)\nres_and = np.bitwise_and(x, y) \nprint (&quot;Bitwise AND result: &quot;, res_and) \n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx= 2\ny= 3\nBitwise AND result:  2\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Bitwise OR operation<\/h2>\n\n\n\n<p>Like AND operation, <strong><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">NumPy<\/a><\/strong> also provides us with <code>numpy.bitwise_or() function<\/code> that enables us to perform the NumPy Bitwise Operations &#8220;OR&#8221; on the data values.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.bitwise_or(num1,num2)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong> <\/p>\n\n\n\n<p>In this example, bitwise_or() function performs OR operation on the two integer values. In the OR operation, if the bits are same i.e. 0\/0, else it returns zero(0), else it returns 1.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nx = 2\ny = 3\n \nprint (&quot;x=&quot;,x)\nprint (&quot;y=&quot;,y)\nres_or = np.bitwise_or(x, y) \nprint (&quot;Bitwise OR result: &quot;, res_or) \n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx= 2\ny= 3\nBitwise OR result:  3\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Bitwise XOR operation<\/h2>\n\n\n\n<p>The XOR operation is one of the NumPy Bitwise Operations. We can perform the operation using numpy.bitwise_xor() function. With this, we can easily perform the bitwise XOR operations on the bit-by-bit data used.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nx = 2\ny = 3\n \nprint (&quot;x=&quot;,x)\nprint (&quot;y=&quot;,y)\nres_xor = np.bitwise_xor(x, y) \nprint (&quot;Bitwise XOR result: &quot;, res_xor) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx= 2\ny= 3\nBitwise XOR result:  1\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Bitwise Invert operation<\/h2>\n\n\n\n<p>The bitwise invert operation is performed using numpy.invert() function. By this, we mean it performs the bit-wise NOT operation on the data bits that are internally worked up on as binary representation format. <\/p>\n\n\n\n<p>In case of signed integers, two&#8217;s complement value is returned.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nx = 2\ny = 3\n \nprint (&quot;x=&quot;,x)\nres = np.invert(x) \nprint (&quot;Bitwise Invert operation result: &quot;, res) \n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx= 2\nBitwise Invert operation result:  -3\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Binary representation<\/h2>\n\n\n\n<p>It is possible for us to explicitly convert the integer values to binary ones, using NumPy module. The<code> binary_repr() <\/code>function enables us to convert an integer data value to binary value easily.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.binary_repr()\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nx = 7\n \nprint (&quot;x=&quot;,x)\nres = np.binary_repr(x) \nprint (&quot;Bitwise representation of x: &quot;, res) \n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>In this example, we have converted the int value &#8216;7&#8217; to its equivalent binary representation.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx= 7\nBitwise representation of x:  111\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.<\/p>\n\n\n\n<p>For more such posts related to Python programming, stay tuned with us.<\/p>\n\n\n\n<p>Till then, Happy Learning!! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, readers! In this article, we will be focusing on 5 NumPy Bitwise Operations that we should know! So, let us get started! To begin with, Bitwise operators help us to perform bit level operations i.e. bit by bit operations through a layer of abstraction enclosed within functions. In the course of the topic, we [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":14357,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-13577","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/13577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=13577"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/13577\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/14357"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=13577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=13577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=13577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}