.bitwise_and()
Anonymous contributor
Published Apr 8, 2025
Contribute to Docs
The .bitwise_and()
method in PyTorch performs an element-wise bitwise AND operation on two tensors or a tensor and a scalar. It returns a new tensor containing 1
where both corresponding bits are 1
, and 0
otherwise. This method is useful for binary operations and low-level bit manipulation.
Syntax
torch.bitwise_and(input, other, *, out=None) → Tensor
Parameters:
input
: The first input tensor.other
: The second input tensor.out
(Optional): The output tensor to store the result.
Return value:
Returns a new tensor containing element-wise bitwise AND values of the input tensors.
Example
The following example demonstrates the usage of the .bitwise_and()
method:
import torch# Define two tensorsa = torch.tensor([5, 6, 7]) # Binary: [101, 110, 111]b = torch.tensor([3, 2, 1]) # Binary: [011, 010, 001]# Perform bitwise AND operationresult = torch.bitwise_and(a, b)print("Bitwise AND Result:")print(result)
The above code produces the following output:
Bitwise AND Result:tensor([1, 2, 1])
All contributors
- Anonymous contributor
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