- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compute bitwise AND, OR and NOT of given input tensors in PyTorch?
To compute bitwise AND of given input tensors we apply torch.bitwise_and(). The input tensors must be of integral or Boolean types. For bool tensors, it computes the logical AND.
To compute bitwise NOT of a given input tensor we apply torch.bitwise_not() method. The input tensors must be of integral or Boolean types. For bool tensors, it computes the logical OR.
To compute the bitwise NOT of a given input tensor we apply torch.bitwise_not() method. The input tensor must be of integral or Boolean types. For bool tensors, it computes the logical NOT.
Syntax
torch.bitwise_and(input1, input2) torch.bitwise_or(input1, input2) torch.bitwise_not(input)
Steps
Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.
import torch
Define torch tensors and print them.
input1 = torch.tensor([4, -2, 3, 0], dtype=torch.int8) input2 = torch.tensor([0, 1, -7, 2], dtype=torch.int8)
Compute bitwise AND, OR or NOT using above defined syntax.
output = torch.bitwise_and(input1, input2)
Print the computed tensor.
print("Bitwise AND:
", output)
Example 1
In the following Python program, we compute bitwise AND of the given input tensors.
# Python 3 program to compute bitwise AND of the given input tensors # Import the required library import torch # define two tensors input1 = torch.tensor([11, -21, 3], dtype=torch.int8) input2 = torch.tensor([-2, 0, 3], dtype=torch.int8) # display the above defined tensors print("Input Tensor 1:
", input1) print("Input Tensor 2:
", input2) # compute the bitwise AND of input1 and input2 output = torch.bitwise_and(input1, input2) # print above computed bitwise and tensor print("Bitwise AND:
", output) print(".................................") # define two tensors input1 = torch.tensor([True, True, False, False]) input2 = torch.tensor([False, True, False, True]) # display the above defined tensors print("Input Tensor 1:
", input1) print("Input Tensor 2:
", input2) # compute the bitwise AND of input1 and input2 output = torch.bitwise_and(input1, input2) # print above computed bitwise and tensor print("Bitwise AND:
", output)
Output
Input Tensor 1: tensor([ 11, -21, 3], dtype=torch.int8) Input Tensor 2: tensor([-2, 0, 3], dtype=torch.int8) Bitwise AND: tensor([10, 0, 3], dtype=torch.int8) ................................. Input Tensor 1: tensor([ True, True, False, False]) Input Tensor 2: tensor([False, True, False, True]) Bitwise AND: tensor([False, True, False, False])
Example 2
In this Python program, we compute bitwise OR of the given input tensors.
# Python 3 program to compute bitwise OR of the given input tensors # Import the required library import torch # define two tensors input1 = torch.tensor([11, -21, 3], dtype=torch.int8) input2 = torch.tensor([-2, 0, 3], dtype=torch.int8) # display the above defined tensors print("Input Tensor 1:
", input1) print("Input Tensor 2:
", input2) # compute the bitwise AND of input1 and input2 output = torch.bitwise_or(input1, input2) # print above computed bitwise and tensor print("Bitwise OR:
", output) print(".................................") # define two tensors input1 = torch.tensor([True, True, False, False]) input2 = torch.tensor([False, True, False, True]) # display the above defined tensors print("Input Tensor 1:
", input1) print("Input Tensor 2:
", input2) # compute the bitwise AND of input1 and input2 output = torch.bitwise_or(input1, input2) # print above computed bitwise and tensor print("Bitwise OR:
", output)
Output
Input Tensor 1: tensor([ 11, -21, 3], dtype=torch.int8) Input Tensor 2: tensor([-2, 0, 3], dtype=torch.int8) Bitwise OR: tensor([ -1, -21, 3], dtype=torch.int8) ................................. Input Tensor 1: tensor([ True, True, False, False]) Input Tensor 2: tensor([False, True, False, True]) Bitwise OR: tensor([ True, True, False, True])
Example 3
In this Python program, we compute bitwise NOT of a given input tensor.
# Python 3 program to compute bitwise NOT of a given input tensor # Import the required library import torch # define input tensors input1 = torch.tensor([11, -21, 3], dtype=torch.int8) # display the above defined tensors print("Input Tensor 1:
", input1) # compute the bitwise NOT output1 = torch.bitwise_not(input1) # print above computed bitwise NOT tensor print("Bitwise NOT:
", output1) # define input tensors input2 = torch.tensor([False, True]) # display the above defined tensors print("Input Tensor 2:
", input2) # compute the bitwise NOT output2 = torch.bitwise_not(input2) # print above computed bitwise NOT tensor print("Bitwise NOT:
", output2)
Output
Input Tensor 1: tensor([ 11, -21, 3], dtype=torch.int8) Bitwise NOT: tensor([-12, 20, -4], dtype=torch.int8) Input Tensor 2: tensor([False, True]) Bitwise NOT: tensor([ True, False])
- Related Articles
- How to compute elementwise logical AND, OR and NOT of given input tensors in PyTorch?
- How to compute the cross entropy loss between input and target tensors in PyTorch?
- PyTorch – How to compute element-wise logical XOR of tensors?
- How to compute the Cosine Similarity between two tensors in PyTorch?
- How to compute the element-wise angle of the given input tensor in PyTorch?
- How to join tensors in PyTorch?
- How to compare two tensors in PyTorch?
- PyTorch – How to compute element-wise entropy of an input tensor?
- How to create tensors with gradients in PyTorch?
- How to compute the Jacobian of a given function in PyTorch?
- How to compute the Hessian of a given scalar function in PyTorch?
- PyTorch – How to compute the norm of a vector or matrix?
- How to perform element-wise addition on tensors in PyTorch?
- How to perform element-wise subtraction on tensors in PyTorch?
- How to perform element-wise multiplication on tensors in PyTorch?
