What do >> and << mean in Python?

What do >> and << mean in Python?

In Python, the >> and << operators are used for bit manipulation, specifically for shifting the bits of a binary number to the right or left, respectively. These operators are often referred to as "right shift" and "left shift" operators.

Right Shift Operator (>>): The >> operator shifts the bits of a number to the right by a specified number of positions. It's equivalent to dividing the number by 2 raised to the power of the shift count. The bits shifted out on the right side are discarded, and zeros are shifted in on the left side.

Example:

num = 8 # Binary: 1000 shifted = num >> 1 # Shifted to the right by 1 position print(shifted) # Output: 4 (Binary: 0100) 

Left Shift Operator (<<): The << operator shifts the bits of a number to the left by a specified number of positions. It's equivalent to multiplying the number by 2 raised to the power of the shift count. The bits shifted out on the left side are discarded, and zeros are shifted in on the right side.

Example:

num = 4 # Binary: 0100 shifted = num << 1 # Shifted to the left by 1 position print(shifted) # Output: 8 (Binary: 1000) 

Bitwise shifting is commonly used in low-level programming and for optimizing certain algorithms. It can be used to efficiently multiply or divide by powers of 2 and to manipulate individual bits within an integer.

Keep in mind that the >> and << operators should not be confused with the > and < comparison operators used for comparing values in Python. The bit manipulation operators work at the binary level, while the comparison operators are used to evaluate conditions.

Examples

  1. "Python >> and << operators":
    These are bitwise shift operators in Python. The << operator shifts the bits of a number to the left by a specified number of positions, while the >> operator shifts the bits to the right. Here's an example:

    x = 5 # 5 in binary is 101 y = x << 2 # Shift left by 2 positions print(y) # Output: 20 

    This code will output 20 because shifting the bits of 5 to the left by 2 positions results in 10100, which is 20 in decimal.

  2. "Python bitwise left shift example":
    The << operator performs a bitwise left shift operation, moving the bits of a number to the left by a specified number of positions. Here's an example:

    x = 4 # 4 in binary is 100 y = x << 1 # Shift left by 1 position print(y) # Output: 8 

    This code will output 8 because shifting the bits of 4 to the left by 1 position results in 1000, which is 8 in decimal.

  3. "Python bitwise right shift example":
    The >> operator performs a bitwise right shift operation, moving the bits of a number to the right by a specified number of positions. Here's an example:

    x = 8 # 8 in binary is 1000 y = x >> 1 # Shift right by 1 position print(y) # Output: 4 

    This code will output 4 because shifting the bits of 8 to the right by 1 position results in 100, which is 4 in decimal.


More Tags

arcmap ssid background-task laravel-5.3 jquery-events workflow-activity feature-extraction uipopovercontroller variable-declaration angular-components

More Python Questions

More Electronics Circuits Calculators

More Auto Calculators

More Chemical thermodynamics Calculators

More Geometry Calculators