torch.Tensor.Tensor.bitwise_not
Bitwise NOT operation (element-wise binary NOT).
Performs bitwise NOT (complement) on each element of an integer tensor. Flips all bits: 0 becomes 1, 1 becomes 0. For unsigned types, equivalent to subtracting from max value + 1. Only works with integer dtypes (int8, int32, uint8, uint32).
Use Cases:
- Creating inverted bit masks
- Flipping all bits in a value
- Boolean inversion at bit level
- Complement operations
- Integer only: Works only with int8, uint8, int32, uint32 dtypes.
- Unary operation: No second tensor needed.
- Sign behavior: For signed types, NOT changes sign significantly.
Returns
Tensor<S, D, Dev>– Integer tensor with bitwise NOT result, same shapeExamples
// Basic bitwise NOT
const a = torch.tensor([0b1010], { dtype: 'uint8' });
a.bitwise_not(); // [0b01010101] = [85]
// Create inverted mask
const mask = torch.tensor([0b11110000], { dtype: 'uint8' });
const inverted = mask.bitwise_not(); // [0b00001111] = [15]See Also
- PyTorch torch.bitwise_not()
- bitwise_and - Bitwise AND
- bitwise_or - Bitwise OR
- bitwise_xor - Bitwise XOR