.bitwise_not()
Published Apr 10, 2025
Contribute to Docs
In PyTorch, the .bitwise_not()
function performs an element-wise bitwise NOT operation on the input tensor. This flips each bit of the tensor’s binary representation, turning 0
to 1
and 1
to 0
. It works for integer tensors (signed or unsigned) and boolean tensors (where it acts as a logical NOT).
Syntax
torch.bitwise_not(input, *, out=None)
Parameters:
input
: A tensor of integer or boolean dtype.out
(Optional): A tensor for storing the output result. Must have the same shape as the input tensor.
Return value:
The .bitwise_not()
function returns a new tensor containing the result of applying the bitwise NOT operation to each element in the input tensor.
Example
The following example illustrates the usage of the .bitwise_not()
function in PyTorch:
import torch# Create a boolean tensortensor_in = torch.tensor([True, False, True])# Apply bitwise NOTresult = torch.bitwise_not(tensor_in)print(result)
The above code produces the following output:
tensor([False, True, False])
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours