
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
Numpy left_shift() Function
The NumPy left_shift() function is used to perform bitwise left shift operations on the elements of an array.
When two arrays or an array and a scalar are given then it shifts the bits of each element in the first array to the left by the number of positions specified in the second array or scalar. The result is equivalent to multiplying the array elements by 2** shift.
- This function supports broadcasting by allowing operations on arrays of different shapes. It's useful in low-level data manipulation such as binary encoding, image processing and operations requiring precise bit control.
- In the context of bitwise operations the negative shift values are generally used with right shift operations where the bits are shifted to the right.
- In the left shift operations the negative values do not have a meaningful interpretation and the behavior may be inconsistent or undefined.
Syntax
Following is the syntax of Numpy left_shift() function −
numpy.left_shift(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature])
Parameters
Following are the Parameters of Numpy left_shift() function −
- x1(array_like): The input array on which the left shift operation is performed.
- x2(array_like or int): The number of bits to shift left each element of x1. It can be a single integer or an array of the same shape as x1.
- out(Optional): This is an optional output array where the result is stored
- where(Optional): A condition to determine where the shift operation should be performed. The result is computed where this condition is True.
- **kwargs: The parameters casting, order, dtype, subok comes under the additional keyword arguments.
Return value
The function returns an array where each element is the result of left shifting the bits corresponding to the original array
Example 1
Following is the basic example of Numpy left_shift() function in which single integer of the array is shifted −
import numpy as np # Shift the bits of the integer 5 to the left by 1 position result = np.left_shift(5, 1) print(result)
Below is the output of left_shift() function −
10
Example 2
We can perform scalar shift uniformly across all elements of the array by shifting the bits of each element to the left by the specified number of positions. Here in this example we are shifting the array by 3 bits −
import numpy as np # Define an array of integers array = np.array([5, 10, 15, 20]) # Define the scalar shift value shift = 3 # Perform the left shift operation result = np.left_shift(array, shift) print("Original array:", array) print("Shifted array:", result)
Following is the output for the above example −
Original array: [ 5 10 15 20] Shifted array: [ 40 80 120 160]
Example 3
Below is the example which performs a left shift on the number 10 by two positions and displays the results which includes the binary representations of the original and shifted values −
import numpy as np print('Left shift of 10 by two positions:') print(np.left_shift(10, 2)) print('Binary representation of 10:') print(np.binary_repr(10, width=8)) print('Binary representation of 40:') print(np.binary_repr(40, width=8))
Following is the output for the above example −
Left shift of 10 by two positions: 40 Binary representation of 10: 00001010 Binary representation of 40: 00101000
Example 2
Here in this example left_shift() does not handle negative shifts in a standard way and the result may not be as expected for left shifts. Instead the results are often not meaningful and may default to the same value as seen in this example −
import numpy as np # Define an array of integers array = np.array([16, 32, 64, 128]) # Define the array of shift values, including negative values shifts = np.array([-1, -2, -3, -4]) # Perform the left shift operation result = np.left_shift(array, shifts) print('Original array:', array) print('Shift values:', shifts) print('Shifted array:', result)
Here is the output of the above example −
Original array: [ 16 32 64 128] Shift values: [-1 -2 -3 -4] Shifted array: [0 0 0 0]