

{"id":79308,"date":"2020-07-20T11:22:00","date_gmt":"2020-07-20T05:52:00","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=79308"},"modified":"2021-05-09T13:13:40","modified_gmt":"2021-05-09T07:43:40","slug":"numpy-bitwise-operators","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/","title":{"rendered":"NumPy Bitwise Operators with Examples"},"content":{"rendered":"<p>NumPy includes a package to perform bitwise operations on the array elements. These NumPy bitwise operators perform bit by bit operations. It performs the function of two-bit values to produce a new value.<\/p>\n<p>There are functions to convert the elements into their binary representation and then apply operations on the bits.<\/p>\n<h2>NumPy Bitwise Operators<\/h2>\n<p>This is a specific package that applies bitwise operations on the binary format of elements. These functions compare the binary value of elements and then produce output. There are 6 basic bitwise operations available in NumPy<\/p>\n<p><strong>1. bitwise_and()-<\/strong> It calculates the bit-wise AND operation between two array elements.<br \/>\n<strong>2. bitwise_or()-<\/strong> It calculates the bit-wise OR operation between two array elements.<br \/>\n<strong>3. invert()-<\/strong> It calculates the bit-wise NOT operation between two array elements.<br \/>\n<strong>4. bitwise_xor()-<\/strong> It calculates the bit-wise OR operation between two array elements.<br \/>\n<strong>5. left_shift()-<\/strong> This operator shifts the bits of the binary representation of the element towards left.<br \/>\n<strong>6. right_shift()-<\/strong> This operator shifts the bits of the binary representation of the element towards the right.<\/p>\n<h3>1. Bitwise AND operator<\/h3>\n<p>The function performs bitwise AND on two array elements. The bitwise function performs an operation on the corresponding bits of the binary representation of the operands i.e. elements. The output of the operation depends on the AND truth table.<\/p>\n<p>If both the corresponding values are 1 only then the output will be 1, otherwise 0. Here 1 can also is equivalent to True and 0 as False. Hence the result will be True only if both the values are True, otherwise, it will result to be False.<\/p>\n<p><strong>Truth Table for AND<\/strong><\/p>\n<table style=\"height: 340px\" width=\"460\">\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">a<\/span><\/td>\n<td><span style=\"font-weight: 400\">b<\/span><\/td>\n<td><span style=\"font-weight: 400\">AND(a,b)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\na=15\r\nb=35\r\nprint(bin(a))\r\nprint(bin(b))\r\nnp.bitwise_and(a,b)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0b1111<br \/>\n0b100011<br \/>\n3<\/div>\n<h3>2. Bitwise OR operator<\/h3>\n<p>The function performs bitwise OR on the binary representation of array elements. The output of the operation depends on the OR truth table. If one or both the corresponding values are 1 then the output will be 1, otherwise 0.<\/p>\n<p>Here 1 is given as True and 0 as False. Hence the result will be True if one or both the values are True, otherwise, it will result to be False.<\/p>\n<p><strong>Truth Table for AND<\/strong><\/p>\n<table style=\"height: 304px\" width=\"452\">\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">a<\/span><\/td>\n<td><span style=\"font-weight: 400\">b<\/span><\/td>\n<td><span style=\"font-weight: 400\">OR(a, b)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\na=15\r\nb=35\r\nprint(bin(a))\r\nprint(bin(b))\r\nnp.bitwise_or(a,b)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0b1111<br \/>\n0b100011<br \/>\n47<\/div>\n<h3>3. NumPy Invert Operator<\/h3>\n<p>This operator is the same as the NOT operator. It calculates the bitwise NOT of the binary representation of the elements. In the case of a signed integer, this function results as its 2\u2019s complement. The output depends on the NOT truth table. It inverts the input value in the output.<\/p>\n<table style=\"height: 170px\" width=\"420\">\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">a<\/span><\/td>\n<td><span style=\"font-weight: 400\">OUTPUT<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\na=15\r\nprint(bin(a))\r\nnp.invert(a)\r\n\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0b1111<br \/>\n-16<\/div>\n<h3>4. NumPy Bitwise XOR operator<\/h3>\n<p>The function performs bitwise XOR on two array elements. We perform the bitwise operation on the corresponding bits of the binary representation of the operands i.e. elements.<\/p>\n<p>The output of the operation depends on the XOR truth table. If both the corresponding values are the same then the output will be 0, otherwise 1.<\/p>\n<table style=\"height: 283px\" width=\"467\">\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">a<\/span><\/td>\n<td><span style=\"font-weight: 400\">b<\/span><\/td>\n<td><span style=\"font-weight: 400\">XOR(a, b)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\na=15\r\nb=35\r\nprint(bin(a))\r\nprint(bin(b))\r\nnp.bitwise_xor(a,b)\r\n\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0b1111<br \/>\n0b100011<br \/>\n44<\/div>\n<h3>5. NumPy Left Shift Operator<\/h3>\n<p>The NumPy left shift operator shifts the binary representation of array elements towards the left. The function takes two parameters \u2013 array and the number of positions to shift. The array shifts towards left by appending zeros to its right.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\nnum = 15\r\nshift = 3\r\nnp.left_shift(num,shift) \r\n\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">120<\/div>\n<h3>6. NumPy Right Shift Operator<\/h3>\n<p>The NumPy right shift operator shifts the binary representation of array elements towards the right. The function takes two parameters \u2013 array and the number of positions to shift.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\nnum = 15\r\nshift = 3\r\nnp.right_shift(num,shift) \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<h2>Summary<\/h2>\n<p>The binary operator performs bitwise operations on the array\u2019s elements. It is a very useful package in NumPy. It has the ease of functionality as there functions even for conversion to binary format. The logics are fed in the functions that produce the correct results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy includes a package to perform bitwise operations on the array elements. These NumPy bitwise operators perform bit by bit operations. It performs the function of two-bit values to produce a new value. There&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":79491,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22401],"tags":[2653,22686,22688,22687],"class_list":["post-79308","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-combinatoric-iterators","tag-numpy-binary-operators","tag-numpy-bitwise-operators","tag-python-numpy-operators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>NumPy Bitwise Operators with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about NumPy Bitwise Operators with syntax and example. These include Bitwise And, Or, XOR, Invert, Left Shift and Right Shift operators.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy Bitwise Operators with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about NumPy Bitwise Operators with syntax and example. These include Bitwise And, Or, XOR, Invert, Left Shift and Right Shift operators.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-20T05:52:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:43:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Bitwise-Operators-in-NumPy.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"NumPy Bitwise Operators with Examples - DataFlair","description":"Learn about NumPy Bitwise Operators with syntax and example. These include Bitwise And, Or, XOR, Invert, Left Shift and Right Shift operators.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/","og_locale":"en_US","og_type":"article","og_title":"NumPy Bitwise Operators with Examples - DataFlair","og_description":"Learn about NumPy Bitwise Operators with syntax and example. These include Bitwise And, Or, XOR, Invert, Left Shift and Right Shift operators.","og_url":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-07-20T05:52:00+00:00","article_modified_time":"2021-05-09T07:43:40+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Bitwise-Operators-in-NumPy.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"NumPy Bitwise Operators with Examples","datePublished":"2020-07-20T05:52:00+00:00","dateModified":"2021-05-09T07:43:40+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/"},"wordCount":607,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Bitwise-Operators-in-NumPy.jpg","keywords":["Combinatoric Iterators","NumPy Binary Operators","Numpy Bitwise operators","Python numpy operators"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/","url":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/","name":"NumPy Bitwise Operators with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Bitwise-Operators-in-NumPy.jpg","datePublished":"2020-07-20T05:52:00+00:00","dateModified":"2021-05-09T07:43:40+00:00","description":"Learn about NumPy Bitwise Operators with syntax and example. These include Bitwise And, Or, XOR, Invert, Left Shift and Right Shift operators.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Bitwise-Operators-in-NumPy.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Bitwise-Operators-in-NumPy.jpg","width":1200,"height":628,"caption":"Bitwise Operators in NumPy"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/numpy-bitwise-operators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"NumPy Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/numpy\/"},{"@type":"ListItem","position":3,"name":"NumPy Bitwise Operators with Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=79308"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79308\/revisions"}],"predecessor-version":[{"id":93075,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79308\/revisions\/93075"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/79491"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=79308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=79308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=79308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}