Pytorch Torch Bitwise_Left_Shift
## PyTorch `torch.bitwise_left_shift`
The `torch.bitwise_left_shift` function in PyTorch computes the element-wise bitwise left shift of an input tensor by a specified number of bits.
Bitwise left shifting an integer by $n$ bits is mathematically equivalent to multiplying the integer by $2^n$ (assuming no overflow occurs).
---
### Syntax
```python
torch.bitwise_left_shift(input, other, *, out=None)
```
#### Parameters
* **`input`** *(Tensor)*: The input tensor containing integer or boolean values.
* **`other`** *(Tensor or Scalar)*: The number of bits to shift to the left. This can be a tensor of the same shape as `input` (or broadcastable to it), or a scalar value.
* **`out`** *(Tensor, optional)*: The output tensor.
#### Return Value
* Returns a new tensor where each element is the result of shifting the corresponding element of `input` to the left by the number of bits specified in `other`.
---
### Code Examples
#### Example 1: Basic Left Shift with a Scalar
This example demonstrates shifting a 1D tensor of 8-bit integers to the left by 1 bit.
```python
import torch
# Create an 8-bit integer tensor
x = torch.tensor([1, 2, 3, 4], dtype=torch.int8)
# Shift left by 1 bit (equivalent to multiplying by 2)
result = torch.bitwise_left_shift(x, 1)
print("Input tensor: ", x)
print("Result tensor:", result) # Output: tensor([2, 4, 6, 8], dtype=int8)
```
#### Example 2: Element-wise Shift Using a Tensor
You can also shift each element of the input tensor by a different number of bits by passing a tensor as the `other` argument.
```python
import torch
x = torch.tensor([1, 1, 1, 1], dtype=torch.int32)
shifts = torch.tensor([0, 1, 2, 3], dtype=torch.int32)
# Shift each element by the corresponding value in the 'shifts' tensor
result = torch.bitwise_left_shift(x, shifts)
print("Result tensor:", result) # Output: tensor([1, 2, 4, 8], dtype=int32)
```
#### Example 3: Using the Operator Alias (`<<`)
PyTorch supports the `<<` operator as a shorthand alias for `torch.bitwise_left_shift`.
```python
import torch
x = torch.tensor([5, 10], dtype=torch.int16)
# Using the << operator
result = x << 2
print("Result tensor:", result) # Output: tensor([20, 40], dtype=int16)
```
---
### Considerations & Rules
1. **Supported Data Types**: This operation is only supported for integral types (e.g., `torch.int8`, `torch.int16`, `torch.int32`, `torch.int64`, `torch.uint8`) and boolean tensors. It is not supported for floating-point types like `torch.float32` or `torch.float64`.
2. **Boolean Tensors**: For boolean tensors, the left shift operation acts as a logical operation where shifting by `0` preserves the value, and shifting by any value greater than `0` results in `False` (since the bit is shifted out of the 1-bit representation).
3. **Overflow**: If the result of the shift exceeds the maximum value that the tensor's data type can represent, the bits will overflow, and the value will wrap around according to standard two's complement integer representation.
YouTip