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])

Updated on: 27-Jan-2022

349 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements