<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Haleem Akintayo</title>
    <description>The latest articles on DEV Community by Haleem Akintayo (@haleemakintayo).</description>
    <link>https://dev.to/haleemakintayo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1942305%2F53ae4fe9-5749-4083-8635-b3871c061754.jpeg</url>
      <title>DEV Community: Haleem Akintayo</title>
      <link>https://dev.to/haleemakintayo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/haleemakintayo"/>
    <language>en</language>
    <item>
      <title>How I Finally Understood Bitwise Operators in Python</title>
      <dc:creator>Haleem Akintayo</dc:creator>
      <pubDate>Mon, 19 Oct 2026 10:49:09 +0000</pubDate>
      <link>https://dev.to/haleemakintayo/how-i-finally-understood-bitwise-operators-in-python-55jl</link>
      <guid>https://dev.to/haleemakintayo/how-i-finally-understood-bitwise-operators-in-python-55jl</guid>
      <description>&lt;h2&gt;
  
  
  A visual mental model for Python's &amp;lt;&amp;lt;, &amp;gt;&amp;gt;, &amp;amp;, |, ^, and ~ operators using numbered boxes and switch panels.
&lt;/h2&gt;

&lt;p&gt;For a long time, bitwise operators were one of those topics I kept meaning to understand.&lt;/p&gt;

&lt;p&gt;I would come across symbols like &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;, and &lt;code&gt;&amp;amp;&lt;/code&gt;, wonder what they meant, and then move on. I knew they had something to do with binary numbers, but I had never taken the time to work through them properly.&lt;/p&gt;

&lt;p&gt;Recently, someone asked me about them, and we started exploring them together.&lt;/p&gt;

&lt;p&gt;At first, the formulas for bitwise operators seemed different from the symbols themselves. Why does shifting left multiply a number by a power of two? Why does shifting right use floor division? And how do operators such as &lt;code&gt;&amp;amp;&lt;/code&gt;, &lt;code&gt;|&lt;/code&gt;, and &lt;code&gt;^&lt;/code&gt; decide which bits to keep?&lt;/p&gt;

&lt;p&gt;The clearest way I found to answer those questions was to start with the basic idea behind binary numbers: a number is represented by bits, and each bit belongs to a position with a particular value. Once that foundation is clear, the shift and comparison operators become much easier to follow.&lt;/p&gt;

&lt;p&gt;To explain it, let us begin with something simpler than Python code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Imagine a row of numbered boxes
&lt;/h2&gt;

&lt;p&gt;Imagine four boxes with these labels:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Box value&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Selected?&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We selected the boxes worth 4 and 1.&lt;/p&gt;

&lt;p&gt;Add their values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 + 1 = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now replace "Yes" with &lt;code&gt;1&lt;/code&gt; and "No" with &lt;code&gt;0&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Box value&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Bit&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That gives us &lt;code&gt;0101&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is &lt;code&gt;5&lt;/code&gt; written in binary.&lt;/p&gt;

&lt;p&gt;A bit is simply a binary digit: &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;. In this example, &lt;code&gt;1&lt;/code&gt; means "include this position's value," while &lt;code&gt;0&lt;/code&gt; means "leave it out."&lt;/p&gt;

&lt;p&gt;Try another number:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Box value&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Bit&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The selected values are 8 and 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;8 + 2 = 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;1010&lt;/code&gt; is &lt;code&gt;10&lt;/code&gt; in binary.&lt;/p&gt;

&lt;p&gt;The position matters. A &lt;code&gt;1&lt;/code&gt; in the box worth 8 contributes more than a &lt;code&gt;1&lt;/code&gt; in the box worth 1.&lt;/p&gt;

&lt;p&gt;Python can show us the binary form of a number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;bin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;   &lt;span class="c1"&gt;# 0b101
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;bin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# 0b1010
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The prefix &lt;code&gt;0b&lt;/code&gt; tells us that the digits after it represent a binary number.&lt;/p&gt;

&lt;p&gt;You may notice that Python writes &lt;code&gt;101&lt;/code&gt; instead of &lt;code&gt;0101&lt;/code&gt;. The leading zero does not change the value, just as &lt;code&gt;005&lt;/code&gt; and &lt;code&gt;5&lt;/code&gt; describe the same amount.&lt;/p&gt;

&lt;p&gt;With that picture in mind, the operators become easier to follow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Left shift moves the bits into more valuable boxes
&lt;/h2&gt;

&lt;p&gt;The left shift operator is &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The symbol has two parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The number on the left is the value whose bits we want to move.&lt;/li&gt;
&lt;li&gt;The number on the right tells Python how many positions to move them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read this as: &lt;em&gt;Take 5 and shift its bits one position to the left.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The number 5 is &lt;code&gt;0101&lt;/code&gt; in our four-box example. Moving every bit one position to the left means that each &lt;code&gt;1&lt;/code&gt; moves into the box immediately to its left, which has twice the value:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Box value&lt;/th&gt;
&lt;th&gt;16&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Before shifting&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;After one left shift&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Before shifting, the selected positions were worth 4 and 1.&lt;/p&gt;

&lt;p&gt;After shifting, those same &lt;code&gt;1&lt;/code&gt; bits occupy positions worth 8 and 2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Before: 4 + 1 = 5
After:  8 + 2 = 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A zero fills the empty position on the right.&lt;/p&gt;

&lt;p&gt;Each bit has moved into a position worth twice as much. That is why one left shift doubles the number.&lt;/p&gt;

&lt;p&gt;Shift again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 20
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;2&lt;/code&gt; means "move the bits two positions to the left," not "multiply by 2 in one step."&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Expression&lt;/th&gt;
&lt;th&gt;Binary result&lt;/th&gt;
&lt;th&gt;Decimal value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Starting number&lt;/td&gt;
&lt;td&gt;&lt;code&gt;101&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;5 &amp;lt;&amp;lt; 1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1010&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;5 &amp;lt;&amp;lt; 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;10100&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The second number is the number of positions to shift. It is not the number to multiply by.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;5 &amp;lt;&amp;lt; 2&lt;/code&gt; does not mean &lt;code&gt;5 × 2&lt;/code&gt;. It means "double 5 twice":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 → 10 → 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives us the formula:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;**&lt;/code&gt; means "raised to the power of."&lt;/p&gt;

&lt;p&gt;For our example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All three expressions give &lt;code&gt;20&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For Python integers, this rule works with nonnegative integer shift counts. Python also allows the number to grow beyond the boxes we draw; those boxes do not impose a fixed size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Right shift moves the bits into less valuable boxes
&lt;/h2&gt;

&lt;p&gt;The right shift operator is &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It works in the opposite direction from &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The number on the left is the value whose bits we want to move.&lt;/li&gt;
&lt;li&gt;The number on the right tells Python how many positions to move them to the right.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read this as: &lt;em&gt;Take 5 and shift its bits one position to the right.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The number 5 is &lt;code&gt;0101&lt;/code&gt;. Moving every bit one position to the right means that each &lt;code&gt;1&lt;/code&gt; moves into the box immediately to its right, which has half the value:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Box value&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Before shifting&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;After one right shift&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;1&lt;/code&gt; in the position worth 4 moves into the position worth 2.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;1&lt;/code&gt; at the far right falls off. There is no position for it to occupy in this integer result.&lt;/p&gt;

&lt;p&gt;We are left with &lt;code&gt;0010&lt;/code&gt;, which is &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That dropped bit explains something important:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 2.5
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Right shift does not return the fractional half.&lt;/p&gt;

&lt;p&gt;Its formula uses floor division:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 2
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 2
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 1
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Floor division rounds the mathematical result down to an integer.&lt;/p&gt;

&lt;p&gt;For positive numbers, it can look like simply removing the decimal part. Negative numbers show why "round down" is the more accurate explanation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# -3
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# -3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Half of &lt;code&gt;-5&lt;/code&gt; is &lt;code&gt;-2.5&lt;/code&gt;. Rounding down gives &lt;code&gt;-3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is also why Python's right shift is described as a signed right shift. The positive-number picture of filling empty positions with zeros needs extra care when negative numbers are involved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now imagine each bit as a switch
&lt;/h2&gt;

&lt;p&gt;Shifts move bits between positions.&lt;/p&gt;

&lt;p&gt;The next three operators compare the bits in matching positions.&lt;/p&gt;

&lt;p&gt;Imagine two panels of switches. Each switch can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt;: off&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt;: on&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will use the numbers &lt;code&gt;5&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Position value&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;First panel — 5&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Second panel — 3&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Each operator follows a different rule for deciding which switches should be on in the result.&lt;/p&gt;

&lt;h2&gt;
  
  
  AND asks whether both switches are on
&lt;/h2&gt;

&lt;p&gt;The bitwise AND operator is &lt;code&gt;&amp;amp;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Its rule is: &lt;em&gt;Keep a position on only when both input switches are on.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Think of a light controlled by two switches, where both must be on before the light comes on.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;5&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Position value&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;AND result&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Only the position worth 1 is on in both numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The question to remember is: &lt;em&gt;Do both numbers have a 1 here?&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  OR asks whether at least one switch is on
&lt;/h2&gt;

&lt;p&gt;The bitwise OR operator is &lt;code&gt;|&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Its rule is: &lt;em&gt;Keep a position on when either input switch is on.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Imagine a light that comes on if either of two switches is on. It stays on when both are on too.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Position value&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;OR result&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The selected values are 4, 2, and 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4 + 2 + 1 = 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Therefore:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The question is: &lt;em&gt;Does at least one number have a 1 here?&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  XOR asks whether the switches are different
&lt;/h2&gt;

&lt;p&gt;The bitwise XOR operator is &lt;code&gt;^&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Its rule is: &lt;em&gt;Keep a position on when exactly one input switch is on.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If both are on, the result is off. If both are off, the result is also off.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Position value&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;XOR result&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The position worth 1 disappears because both numbers have a &lt;code&gt;1&lt;/code&gt; there.&lt;/p&gt;

&lt;p&gt;The positions worth 4 and 2 remain because their input bits differ.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The question is: &lt;em&gt;Are the two bits different here?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Together, the three rules look like this:&lt;/p&gt;

&lt;p&gt;| First bit | Second bit | AND (&lt;code&gt;&amp;amp;&lt;/code&gt;) | OR (&lt;code&gt;\|&lt;/code&gt;) | XOR (&lt;code&gt;^&lt;/code&gt;) |&lt;br&gt;
| :---: | :---: | :---: | :---: | :---: |&lt;br&gt;
| 0 | 0 | 0 | 0 | 0 |&lt;br&gt;
| 0 | 1 | 0 | 1 | 1 |&lt;br&gt;
| 1 | 0 | 0 | 1 | 1 |&lt;br&gt;
| 1 | 1 | 1 | 1 | 0 |&lt;/p&gt;
&lt;h2&gt;
  
  
  NOT flips the switches, with one Python detail
&lt;/h2&gt;

&lt;p&gt;The bitwise NOT operator is &lt;code&gt;~&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It changes &lt;code&gt;0&lt;/code&gt; bits to &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; bits to &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Looking at four switches, we might expect this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Position value&lt;/th&gt;
&lt;th&gt;8&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Original bits&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Flipped bits&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The flipped four-bit pattern is &lt;code&gt;1010&lt;/code&gt;, or &lt;code&gt;10&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But Python gives us this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# -6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Python has not agreed to stop flipping after four bits.&lt;/p&gt;

&lt;p&gt;The four boxes were our drawing. An ordinary Python integer is not restricted to those four positions.&lt;/p&gt;

&lt;p&gt;Python defines integer bitwise operations using a model called two's complement with infinitely many sign bits. This is a rule for how the operations behave, not a claim that Python physically stores infinitely many bits.&lt;/p&gt;

&lt;p&gt;For now, the useful identity is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;~5 = -(5 + 1) = -6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try a few more:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# -1
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# -2
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# -6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we specifically want the result from flipping only our four switches, we can keep just the lowest four bits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mb"&gt;0b1111&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;0b1111&lt;/code&gt; acts as a mask. Think of it as a window with four open spaces: &lt;code&gt;AND&lt;/code&gt; lets those four positions through and clears the higher ones.&lt;/p&gt;

&lt;p&gt;That gives us the four-bit result we originally pictured.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where would I actually use this?
&lt;/h2&gt;

&lt;p&gt;Imagine an application with three settings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allow reading.&lt;/li&gt;
&lt;li&gt;Allow editing.&lt;/li&gt;
&lt;li&gt;Allow sharing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We could give each setting its own bit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;READ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;   &lt;span class="c1"&gt;# 001 (1 &amp;lt;&amp;lt; 0)
&lt;/span&gt;&lt;span class="n"&gt;EDIT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;   &lt;span class="c1"&gt;# 010 (1 &amp;lt;&amp;lt; 1)
&lt;/span&gt;&lt;span class="n"&gt;SHARE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;  &lt;span class="c1"&gt;# 100 (1 &amp;lt;&amp;lt; 2)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To allow reading and sharing, combine the flags with &lt;code&gt;OR&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;settings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;READ&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;SHARE&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;5&lt;/code&gt; represents &lt;code&gt;101&lt;/code&gt;: sharing is on, editing is off, and reading is on.&lt;/p&gt;

&lt;p&gt;To check whether editing is enabled, use &lt;code&gt;AND&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;EDIT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# False
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To check reading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;READ&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;AND&lt;/code&gt; checks whether the relevant bit is present. &lt;code&gt;bool()&lt;/code&gt; turns the result into &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The switch analogy now describes something an application might actually store.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two easy mistakes to avoid
&lt;/h2&gt;

&lt;p&gt;First, &lt;code&gt;&amp;amp;&lt;/code&gt; and &lt;code&gt;and&lt;/code&gt; are different operators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# 1
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;&amp;amp;&lt;/code&gt; compares the bits.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;and&lt;/code&gt; checks truthiness and short-circuits. Since &lt;code&gt;5&lt;/code&gt; is truthy, it evaluates and returns the second operand, &lt;code&gt;3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Second, &lt;code&gt;^&lt;/code&gt; means XOR in Python. Powers use &lt;code&gt;**&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 1
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That distinction matters when using the shift formulas.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would tell someone learning this now
&lt;/h2&gt;

&lt;p&gt;Start with the numbered boxes.&lt;/p&gt;

&lt;p&gt;Understand why &lt;code&gt;0101&lt;/code&gt; represents &lt;code&gt;5&lt;/code&gt;. Then move the bits and watch how their position values change. After that, compare two rows of switches using &lt;code&gt;AND&lt;/code&gt;, &lt;code&gt;OR&lt;/code&gt;, and &lt;code&gt;XOR&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The formulas become easier to remember when you can explain them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A left shift moves bits into positions worth twice as much.&lt;/li&gt;
&lt;li&gt;A right shift moves bits into positions worth half as much, with floor division determining the integer result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Working through this with someone finally got me to spend time on a topic I had kept postponing.&lt;/p&gt;

&lt;p&gt;If you have been doing the same, try this before running any code:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What will &lt;code&gt;6 &amp;amp; 3&lt;/code&gt; produce?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
  Click to reveal the answer
  &lt;p&gt;6 in binary: 110&lt;br&gt;
3 in binary: 011&lt;/p&gt;

&lt;p&gt;AND result: 010&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer: 2&lt;/strong&gt;&lt;/p&gt;



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Write both numbers in binary. Line up their bits. Keep only the positions where both have a &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then let Python check your answer.&lt;/p&gt;




&lt;h3&gt;
  
  
  Let's talk in the comments 💬
&lt;/h3&gt;

&lt;p&gt;I wrote this guide because for a long time, I skipped right past symbols like &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;amp;&lt;/code&gt;, and &lt;code&gt;~&lt;/code&gt; until someone asked me about them and we worked through the mental model together. I hope picturing numbered boxes and switch panels saves you the time I spent postponing it!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What did you get for &lt;code&gt;6 &amp;amp; 3&lt;/code&gt;?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do you have any questions about how Python handles shifts, masks, or &lt;code&gt;~&lt;/code&gt;?&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Are there other "postponed" Python topics you'd like to explore together next?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Drop your questions or thoughts in the comments below , I read and reply to every one!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/python/python_operators.asp" rel="noopener noreferrer"&gt;W3Schools — Python Bitwise Operators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/library/stdtypes.html#bitwise-operations-on-integer-types" rel="noopener noreferrer"&gt;Python documentation — Bitwise Operations on Integer Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.python.org/3/reference/expressions.html#shifting-operations" rel="noopener noreferrer"&gt;Python language reference — Shifting Operations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>Why I Want to Be a Front-End Developer and How HNG Will Help Me Achieve My Goals</title>
      <dc:creator>Haleem Akintayo</dc:creator>
      <pubDate>Thu, 06 Feb 2025 21:14:48 +0000</pubDate>
      <link>https://dev.to/haleemakintayo/why-i-want-to-be-a-front-end-developer-and-how-hng-will-help-me-achieve-my-goals-1fj7</link>
      <guid>https://dev.to/haleemakintayo/why-i-want-to-be-a-front-end-developer-and-how-hng-will-help-me-achieve-my-goals-1fj7</guid>
      <description>&lt;p&gt;Ever since I was introduced to the world of web design in high school, I have been captivated by the vibrant interplay of creativity and technology. The moment I wrote my first line of HTML, I realized that front-end development was not just about coding—it was about crafting immersive digital experiences. I became fascinated by the idea that every color, every font, and every layout decision has the power to transform how users interact with a website. This realization ignited my passion for front-end development and set me on a path to build user-friendly, responsive interfaces.&lt;/p&gt;

&lt;p&gt;Joining the HNG internship is a significant milestone in my journey. I chose HNG because it offers a dynamic learning environment, mentorship from industry experts, and real-world projects that challenge my abilities. At HNG, I know I will be surrounded by a community of like-minded developers who are eager to innovate and share knowledge. This supportive ecosystem is exactly what I need to bridge the gap between theoretical knowledge and practical application.&lt;/p&gt;

&lt;p&gt;During this internship, my primary goal is to master modern front-end technologies—ranging from responsive web design and dynamic JavaScript to advanced CSS techniques. I plan to achieve these goals by actively engaging in team projects, seeking out constructive feedback, and dedicating time to personal experiments with emerging tools and frameworks. I am particularly excited about learning from industry leaders and exploring resources such as &lt;a href="https://hng.tech/hire/html-developers" rel="noopener noreferrer"&gt;Hire HTML Developers&lt;/a&gt; and &lt;a href="https://hng.tech/hire/css-developers" rel="noopener noreferrer"&gt;Hire CSS Developers&lt;/a&gt; to stay up-to-date with best practices and new trends.&lt;/p&gt;

&lt;p&gt;Ultimately, my aspiration is to create visually stunning, intuitive websites that provide exceptional user experiences. With the support of HNG and my unwavering passion for front-end development, I am confident that this internship will pave the way for a fulfilling and successful career.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
