{"id":74539,"date":"2019-12-21T09:42:47","date_gmt":"2019-12-21T04:12:47","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=74539"},"modified":"2024-08-22T20:15:57","modified_gmt":"2024-08-22T14:45:57","slug":"python-bitwise-operators","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/","title":{"rendered":"Python Bitwise Operators &#8211;  Start operating on the bits of numbers"},"content":{"rendered":"<p>Today, we are going to understand python bitwise operators.<\/p>\n<p>Python bitwise operators include <strong>And<\/strong>, <strong>Or<\/strong>, <strong>Xor<\/strong>, <strong>1\u2019s complement<\/strong>, <strong>Left-shift<\/strong>, and <strong>Right-shift<\/strong>. These operators are called <strong>bitwise<\/strong> because they operate on bits of the number. A number in binary is represented in the form of <strong>zeroes \u20180\u2019<\/strong> and <strong>ones \u20181\u2019<\/strong>.<\/p>\n<p>For <strong>example:<\/strong>\u00a0the number <strong>1234<\/strong> in binary is represented as <strong>\u201810011010010\u2019<\/strong>.<\/p>\n<p>The bitwise operators work on these stream of bits which means that they work directly at the <strong>binary<\/strong> level. It may sound scary, but actually, it\u2019s very easy to understand once you know how binary numbers are represented.<\/p>\n<h3>Types of Python Bitwise Operators<\/h3>\n<p>We have the following bitwise operators that are supported by Python &#8211;<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/types-of-bitwise-operators.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74683 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/types-of-bitwise-operators.jpg\" alt=\"types of biwise operators in python\" width=\"657\" height=\"304\" \/><\/a><\/p>\n<p>Let us go through each of them in a little bit more detail.<\/p>\n<h4>1. Bitwise And (&amp;)<\/h4>\n<p>The bitwise AND operator takes <strong>two arguments<\/strong> and performs <strong>AND<\/strong> operation on the operands bit by bit. First, let\u2019s recall how And operation on binary digits are implemented.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/implementation-of-and-operation-on-binary-digits.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74697 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/implementation-of-and-operation-on-binary-digits.jpg\" alt=\"implementation of bitwise AND operation\" width=\"341\" height=\"272\" \/><\/a><\/p>\n<p>As you can observe, the operation will only be 1 when both operands are <strong>1<\/strong> otherwise it remains <strong>0<\/strong>.<\/p>\n<p><strong>Example: <\/strong><\/p>\n<p>19 &amp; 7 will give us 5.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/example-of-bitwise-and-operation.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74685 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/example-of-bitwise-and-operation.jpg\" alt=\"example of python bitwise operators\" width=\"402\" height=\"245\" \/><\/a><\/p>\n<h4>2. Bitwise Or (|)<\/h4>\n<p>The bitwise OR operator performs a logical OR operation on each bit of the operands. It takes two binary numbers and compares their corresponding bits. If either bit is 1, the result is 1; if both bits are 0, the result is 0. This operator is useful for setting specific bits in a number or combining flags and binary masks. For example, if you use bitwise OR to combine multiple binary flags, each bit in the result represents the presence of one or more flags.<\/p>\n<p>Here\u2019s a truth table for it.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/bitwise-or-operations.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74686 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/bitwise-or-operations.jpg\" alt=\"Python bitwise OR operators\" width=\"325\" height=\"238\" \/><\/a><\/p>\n<p><strong>Example: <\/strong><\/p>\n<p>12 | 9 will give us 13.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/example-of-bitwise-or-operation.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74687 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/example-of-bitwise-or-operation.jpg\" alt=\"example of bitwise OR operator\" width=\"464\" height=\"208\" \/><\/a><\/p>\n<h4>3. Bitwise Xor (^)<\/h4>\n<p>The bitwise Xor also takes<strong> two operands<\/strong> and performs <strong>Xor<\/strong> operation on the binary digits. The Xor operation gives <strong>0<\/strong> as a result when both operands are the <strong>same<\/strong> and <strong>1<\/strong> when operands are <strong>different<\/strong>. This means it effectively toggles the bits, flipping 1s to 0s and 0s to 1s, wherever the operands differ.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/bitwise-Xor-operations.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74688 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/bitwise-Xor-operations.jpg\" alt=\"bitwise Xor operators in Python\" width=\"325\" height=\"238\" \/><\/a><\/p>\n<p><strong>Example: <\/strong><\/p>\n<p>8 ^ 10 will give us 2.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/example-of-bitwise-Xor-operation.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74689 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/example-of-bitwise-Xor-operation.jpg\" alt=\"bitwise Xor example\" width=\"500\" height=\"197\" \/><\/a><\/p>\n<p>One property of bitwise Xor is that it will return <strong>0<\/strong> only if both the numbers are the <strong>same<\/strong>.<\/p>\n<p>5^5 = 0<\/p>\n<p>128^128 = 0<\/p>\n<h4>4. Bitwise 1\u2019s complement (~)<\/h4>\n<p>The bitwise 1&#8217;s complement operator works on a single operand and inverts all the bits of the number. This means that each 0 in the binary representation is changed to 1, and each 1 is changed to 0. It effectively flips every bit in the binary representation of the number, which is useful for operations like calculating the binary negation of a number. It is used to get the <strong>1\u2019s complement<\/strong> of a number. Let\u2019s see the example of how 1\u2019s complement works.<\/p>\n<p>~5 = -6<\/p>\n<p>It flips all the bits until it reaches first zero from the right, so <strong>~x<\/strong> is the same as <strong>-(x+1)<\/strong><\/p>\n<p>~10 = -11<\/p>\n<p>~258 = -259<\/p>\n<h4>5. Left-Shift (&lt;&lt;)<\/h4>\n<p>The left shift operator <strong>shifts<\/strong> the bits of the numbers by a specified number of places. It <strong>adds zeroes<\/strong> to the <strong>empty least significant<\/strong> places.<\/p>\n<p><strong>Syntax :<\/strong><\/p>\n<p>digit &lt;&lt; number_of_places_to_shift<\/p>\n<p>This will be better understood with the example.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<p>1&lt;&lt;2 = 4<\/p>\n<p><strong>1<\/strong> is represented as <strong>\u20181\u2019<\/strong><\/p>\n<p>We shift the digits to <strong>left<\/strong> up to <strong>two places<\/strong>. So, it will be<strong> \u2018100\u2019<\/strong> in binary and<strong> 4<\/strong> in decimal.<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<p>10&lt;&lt;4 = 160<\/p>\n<p><strong>10<\/strong> in binary is <strong>\u20181010\u2019<\/strong><\/p>\n<p>Shifting 4 digits to the left will make it<strong> \u20181010 0000\u2019<\/strong> which is <strong>160<\/strong> in decimal.<\/p>\n<h4>6. Right-Shift (&gt;&gt;)<\/h4>\n<p>This is similar to the left shift, but instead of shifting digits to the left we are <strong>shifting<\/strong> them to the <strong>right<\/strong>. In this process, some of the <strong>bits<\/strong> are <strong>lost<\/strong>.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<p>10&gt;&gt;2 = 2<\/p>\n<p>Let\u2019s see how this works.<\/p>\n<p>So, <strong>10<\/strong> in binary is<strong> \u201c1010\u201d<\/strong>.<\/p>\n<p>By shifting <strong>\u201c1010\u201d<\/strong> to <strong>2 places<\/strong> to the <strong>right<\/strong> we will lose our 2 rightmost digits and we will only be left with <strong>\u201c10\u201d<\/strong>.<\/p>\n<p>In decimal,<strong> \u201c10\u201d<\/strong> represents <strong>2<\/strong> and that\u2019s our answer.<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<p>25&gt;&gt;2 = 6<\/p>\n<p><strong>25<\/strong> in binary is <strong>\u201c11001\u201d<\/strong>. We right shift them up to <strong>2 places<\/strong> so, we are now left with<strong> \u201c110\u201d<\/strong> which is <strong>6<\/strong>.<\/p>\n<h3>Summary<\/h3>\n<p>That was all about TechVidvan&#8217;s python bitwise operators. Usually, they are not used in <strong>normal programming<\/strong> but in some cases, they are very useful like bit <strong>manipulations<\/strong>, <strong>encryptions<\/strong>, and <strong>compressions<\/strong>.<\/p>\n<p>Operations on Bit operators are <strong>faster<\/strong> than the other operators.<\/p>\n<p>Knowing how to use them effectively will make you a better programmer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, we are going to understand python bitwise operators. Python bitwise operators include And, Or, Xor, 1\u2019s complement, Left-shift, and Right-shift. These operators are called bitwise because they operate on bits of the number.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":74748,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1118,1119,1120,1121,1113,1123,1125],"class_list":["post-74539","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-bitwise-and-in-python","tag-bitwise-or-in-python","tag-bitwise-xor-in-python","tag-left-shift-operator-in-python","tag-python-bitwise-operators","tag-right-shift-operator-in-python","tag-types-of-python-bitwise-operators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Bitwise Operators - Start operating on the bits of numbers - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Bitwise Operators - Get ready to understand the concept of bitwise operators in Python. See its various types with syntax &amp; examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Bitwise Operators - Start operating on the bits of numbers - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Bitwise Operators - Get ready to understand the concept of bitwise operators in Python. See its various types with syntax &amp; examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-21T04:12:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-22T14:45:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-bitwise-operators-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Bitwise Operators - Start operating on the bits of numbers - TechVidvan","description":"Python Bitwise Operators - Get ready to understand the concept of bitwise operators in Python. See its various types with syntax & examples.","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:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/","og_locale":"en_US","og_type":"article","og_title":"Python Bitwise Operators - Start operating on the bits of numbers - TechVidvan","og_description":"Python Bitwise Operators - Get ready to understand the concept of bitwise operators in Python. See its various types with syntax & examples.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2019-12-21T04:12:47+00:00","article_modified_time":"2024-08-22T14:45:57+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-bitwise-operators-1.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Bitwise Operators &#8211; Start operating on the bits of numbers","datePublished":"2019-12-21T04:12:47+00:00","dateModified":"2024-08-22T14:45:57+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/"},"wordCount":704,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-bitwise-operators-1.jpg","keywords":["Bitwise And in python","Bitwise Or in python","bitwise xor in python","left shift operator in python","python bitwise operators","right shift operator in python","types of python bitwise operators"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/","url":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/","name":"Python Bitwise Operators - Start operating on the bits of numbers - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-bitwise-operators-1.jpg","datePublished":"2019-12-21T04:12:47+00:00","dateModified":"2024-08-22T14:45:57+00:00","description":"Python Bitwise Operators - Get ready to understand the concept of bitwise operators in Python. See its various types with syntax & examples.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-bitwise-operators-1.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-bitwise-operators-1.jpg","width":802,"height":420,"caption":"bitwise operators in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-bitwise-operators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Bitwise Operators &#8211; Start operating on the bits of numbers"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=74539"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74539\/revisions"}],"predecessor-version":[{"id":447697,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74539\/revisions\/447697"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/74748"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=74539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=74539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=74539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}