torch.bitwise_and
function bitwise_and<S1 extends Shape, S2 extends Shape>(input: Tensor<S1>, other: Tensor<S2>): Tensor<BroadcastShape<S1, S2>>Element-wise bitwise AND: sets bit to 1 only where both operands have 1.
Performs logical AND on individual bits of two integers. Essential for:
- Bit masking: Extract specific bits from values
- Flag operations: Check if multiple bits/flags are set
- Bit manipulation: Isolate specific bit patterns
- Network packets: Mask IP addresses, extract fields
- Graphics: Combining color channels with masks
- Compression: Extracting compressed data fields
- Hardware control: Setting register bits with masks
Binary behavior: For each bit position, result is 1 only if both inputs have 1 at that position.
Integer-only: Works with int32, int8, uint8, uint32, etc. Not valid for floats.
- Integer tensors only: Must use int8, uint8, int32, uint32 dtypes
- Bitwise operation: Works on individual bits, not numeric values
- Broadcasting: Shapes broadcast like regular operators
- Non-destructive: Both operands left unchanged
- Commutative: a AND b = b AND a (order doesn't matter)
- Associative: (a AND b) AND c = a AND (b AND c)
- Integer-only: Cannot use with float32 or float64
- Signed/unsigned: Be careful with sign bits in signed integers
- Overflow: Result never larger than operands
Parameters
inputTensor<S1>- First integer tensor (any shape)
otherTensor<S2>- Second integer tensor (must be broadcastable with input)
Returns
Tensor<BroadcastShape<S1, S2>>– Integer tensor with shape = broadcast(input, other)Examples
// Basic bitwise AND
const x = torch.tensor([5, 6, 7], { dtype: 'int32' }); // Binary: 101, 110, 111
const y = torch.tensor([3, 3, 3], { dtype: 'int32' }); // Binary: 011, 011, 011
torch.bitwise_and(x, y); // [1, 2, 3] = Binary: 001, 010, 011// Bit masking: extract specific bits (e.g., lower 4 bits)
const data = torch.tensor([0xFF, 0xAB, 0x12], { dtype: 'uint8' });
const mask = torch.tensor([0x0F, 0x0F, 0x0F], { dtype: 'uint8' }); // 00001111
const lower_bits = torch.bitwise_and(data, mask);
// [0x0F, 0x0B, 0x02] - keeps only lower 4 bits// Flag checking: which flags are set in both?
const user1_perms = torch.tensor([0b11010101], { dtype: 'uint8' }); // rwx-w--x-
const user2_perms = torch.tensor([0b10110100], { dtype: 'uint8' }); // rw-r-w---
const common = torch.bitwise_and(user1_perms, user2_perms);
// [0b10010100] - permissions both users have// Network: extract subnet from IP address
const ip_address = torch.tensor([192, 168, 1, 100], { dtype: 'uint8' });
const subnet_mask = torch.tensor([255, 255, 255, 0], { dtype: 'uint8' });
const network = torch.bitwise_and(ip_address, subnet_mask);
// [192, 168, 1, 0] - network address// Checking if specific bits are set
const value = torch.tensor([0b11010110], { dtype: 'uint8' });
const bit3_mask = torch.tensor([0b00001000], { dtype: 'uint8' });
const has_bit3 = torch.bitwise_and(value, bit3_mask);
// [0b00000000] means bit 3 is NOT setSee Also
- PyTorch torch.bitwise_and(input, other)
- bitwise_or - OR: set bit to 1 if either operand has 1
- bitwise_xor - XOR: set bit to 1 if operands differ
- bitwise_not - NOT: invert all bits
- bitwise_left_shift - Left shift: multiply by powers of 2
- bitwise_right_shift - Right shift: divide by powers of 2