torch.Tensor.Tensor.bitwise_and
Tensor.bitwise_and(other: number): Tensor<S, D, Dev>Tensor.bitwise_and(other: Tensor | number): Tensor<DynamicShape, D, Dev>Bitwise AND operation (element-wise binary AND).
Performs bitwise AND between corresponding elements of two integer tensors. Each bit position is AND-ed independently: result bit is 1 only if both input bits are 1. Only works with integer dtypes (int8, int32, uint8, uint32).
Use Cases:
- Bitwise masking (selecting specific bits)
- Checking bit flags
- Binary feature operations
- Low-level integer algorithms
- Signal/protocol processing
- Integer only: Works only with int8, uint8, int32, uint32 dtypes.
- Element-wise: Each element is AND-ed independently.
- Broadcasting: Other broadcasts with self.
Parameters
othernumber- Integer tensor to AND with (broadcastable)
Returns
Tensor<S, D, Dev>– Integer tensor with bitwise AND result, same shapeExamples
// Basic bitwise AND
const a = torch.tensor([0b1100, 0b1010], { dtype: 'int32' });
const b = torch.tensor([0b1010, 0b1100], { dtype: 'int32' });
a.bitwise_and(b); // [0b1000, 0b1000] = [8, 8]
// Bit masking
const value = torch.tensor([255], { dtype: 'uint8' });
const mask = torch.tensor([0x0F], { dtype: 'uint8' }); // Keep lower 4 bits
value.bitwise_and(mask); // [15] - only lower nibble
// Check flags
const flags = torch.tensor([0b11010110], { dtype: 'uint8' });
const check_bit_2 = flags.bitwise_and(torch.tensor([0b00000100], { dtype: 'uint8' }));
// Result != 0 means bit 2 is setSee Also
- PyTorch torch.bitwise_and()
- bitwise_or - Bitwise OR
- bitwise_xor - Bitwise XOR
- bitwise_not - Bitwise NOT