# Understanding Binary Right and Left Shift in Python the Easy Way

**URL:** <https://community.f5.com/t/understanding-binary-right-and-left-shift-in-python-the-easy-way/66059>\
**Category:** F5 Technical Articles\
**Tags:** big-ip, devops, python\
**Created:** [August 29, 2019, 11:32am UTC](https://community.f5.com/t/understanding-binary-right-and-left-shift-in-python-the-easy-way/66059 "2019-08-29T11:32:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodrigo\_Albuque](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@Rodrigo\_Albuque](https://community.f5.com/u/Rodrigo_Albuque)\
**Post date:** [August 29, 2019, 11:32am UTC](https://community.f5.com/t/understanding-binary-right-and-left-shift-in-python-the-easy-way/66059/1 "2019-08-29T11:32:14Z")

</div>

## Things to Keep in Mind

- Most significant bit: left-most bit
- Least significant bit: right-most bit
- Think of Python reading your decimal number in its binary form under the hood

## Understanding the Trick to Check if Last Bit is 1

The & operator returns 1 every time 1 is also compared to 1 and zero for 1 compared to anything else:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/f/ffd6e80397e0da584fca2e74aa767ae21732c3db.png)

Keep this in mind as we’re going to use it in our code.

## Right and Left Shift Operators in Python

Let’s take decimal 5 as our example.

5 in binary is 0101:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/3X/7/e/7e21a64350a33f693825e05595cf3b556bec0974.png)

When we ask Python to right shift 5, python will do this operation in binary:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/2/2ea04893834b6160e055f63a317a67de4d6cda36.png)

The above is the same as moving 0101 (5 in decimal) to the right so it’s now 0010 (2 in decimal).

If you move 2 one bit to the left it’s 4 because when we move the one from 00 **1** 0 it’s now 0 **1** 00:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/6/6ff2e0683505e8bc020a3e6214dc8af8f6126b13.png)

## Understanding that anything that is not zero is True in boolean

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/3X/6/e/6e30321a7ab5f46d8f842716a68ef47f1d8eb35e.png)

You need to know this because our while loop will exit when we keep right shifting 5 and its value reaches 0.

## Our Code

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/5/564febfe74ee623da83523ccc230f4abc56c8b37.png)

## What does above code do

Our little code here will count the number of binary 1’s given a decimal number as input using right shift operation previously described.

Our while loop ends when number = 0 and counter variable&nbsp;increments every time least significant bit = 1. Easy eh?

Let’s use 5 as an example. A better way to understand it is to think of our code like this:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/8/8673db8bda69519936ea6c012e1507cb48258407.png)

After we discard “1”, we repeat the above with 0010 and so on, until we number =&nbsp;0.

## Running our Code

Let’s run it but we already know that the number of 1’s in 5 (0101 in binary) is 2:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/9/9b24bdca00d4a19a100f0a7c88897926ecfad30f.png)

In case you didn’t understand yet you can follow along the bonus section with step by step explanation about the code.

## BONUS! Going through our Code Step by Step

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/5/564febfe74ee623da83523ccc230f4abc56c8b37.png)

_counter_&nbsp;variable starts at 0 and we enter the while loop as variable&nbsp;_number_&nbsp;(5) is not yet zero.

counter = 0

number = 5

The first operation will be&nbsp;_number_&nbsp;(5) & 1 or 0101 & 0001 in binary:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/3X/5/a/5a949eaa6f899175e5b4e6e608ee6ab6e2d48843.png)

so&nbsp;_counter_&nbsp;is now whatever it was (0) plus 1, so&nbsp;_counter = 1_.

Next, we shift&nbsp;_number_&nbsp;variable (currently 5) by 1:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/6/65980baa637c68fb4f86617103f7f33f7d5eea35.png)

We now come back to our while loop again and because&nbsp;_number_&nbsp;is still not zero yet we keep going.

counter = 1

number = 2

The first operation will be&nbsp;_number_&nbsp;(2) & 1 or 0010 & 0001 in binary:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/3X/c/2/c232e34782b21ba0f2f26e422e5cb2fd7b84842c.png)

so&nbsp;_counter_&nbsp;is now whatever it was (1) plus 0, so counter does not change (still 1).

Next, we shift&nbsp;_number_&nbsp;again (currently 2) by 1:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/b/bb95fd392bcf37c93946f252b5add2b7844135db.png)

Now we’ve got the following:

counter = 1

number = 1

The first operation will be&nbsp;_number_&nbsp;(1) & 1 or 0001 & 0001 in binary:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/b/b58c30f36ab27ea3d182bbb131769de01692f7e8.png)

so&nbsp;_counter_&nbsp;is now whatever it was (1) plus 1, so&nbsp;_counter_&nbsp;is now 2.

We shift&nbsp;_number_&nbsp;again (currently 1) by 1:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/e/e205b7b12b1d45a153c98c6926a4545e3d856329.png)

We come back to our while loop again but this time we exit because&nbsp;_number_&nbsp;is now 0 and we return&nbsp;_counter_&nbsp;(2).

This is the result our code again:

 ![image_281135.png](https://d20hrnpixdzcsd.cloudfront.net/original/2X/e/e82969ac8f9bc09b5c81b1af150859e50e762fa6.png)
