Bitwise Right Shift and Left Shift Operators in Python || Lesson 19 || Python For Beginners ||

Опубликовано: 03 Август 2026
на канале: Wisdomers - Placements & Programming
No
0

In this class we understand bitwise right shift and left shit operators in python

0:00 intro
0:42 example on left shift
2:13 right shift example and demonstration
In this session, we are diving deep into Bitwise Shift Operators in Python.
While these operators are often considered tricky, they are a frequent highlight in technical placement exams and coding interviews.
Let’s break down the logic step-by-step to ensure you have a competitive edge.


Shift operations are categorized into two fundamental types: Left Shift and Right Shift. While they serve different mathematical purposes, both manipulate data at the bit level. Let’s break down their behavior one by one through illustrative examples.

Consider an integer x = 9. In 8-bit binary notation, 9 is represented as shown in video.
. In Python, the left-shift operation is written as z = x ltlt 1. The left-shift operator is denoted by two 'less-than' symbols. To understand the underlying mechanics, let’s visualize how this value is structured within a memory block.

Shot4
The operation x lt lt 1 shifts every bit in the variable x one position to the left. When this shift occurs, the vacancy created at the rightmost position is automatically filled with a zero. Every bit then advances to the next higher power—from the first position to the second, the second to the third, and so on. Consequently, the original leftmost bit is pushed beyond the memory boundary and is discarded.

Shot5
Observe the result of our operation: the binary value for 9 has been transformed into 18, effectively doubling the original number. This illustrates a fundamental rule: a left shift by one is mathematically equivalent to multiplying by 2 power 1. Mastering this shortcut will allow you to solve complex bitwise problems in competitive exams with much greater speed.

Shot6

Now, let's transition to the Right Shift operator. this is known as an Arithmetic Right Shift. Because its operations differs significantly from left shift, it requires your full attention. Stay focused as we break down exactly how these bits move.

Shot7
Let’s take the integer x=9. In an arithmetic right shift, the process is governed by the sign bit. First, the leftmost bit—our sign bit—is preserved to maintain the value's sign. Every subsequent bit is then shifted one position to the right, starting from the left. As this migration occurs, the original rightmost bit is pushed beyond the memory boundary and is discarded.

Shot8
The right-shift operation is denoted by double 'greater-than' symbols (gt gt). Observe the transformation: the initial value of 9 has been reduced to 4. This demonstrates that a right shift by one bit is mathematically equivalent to floor division by two. Since 9 divided by 2 equals 4.5, applying the floor function results in exactly 4.

Shot9
To wrap up, look at how we scale these operations: z = x gtgt 2 shifts the bits twice to the right, and z = x ltlt 3 shifts them three times to the left. Mastering these multi-bit shifts is key to optimizing low-level code. I hope this explanation has cleared all your doubts regarding bitwise operators. Thank you for staying with us until the end.

visit our learning agent at www.wisdomers.in for quizzes and projects