torch.bitwise_not
function bitwise_not<S extends Shape, D extends DType = DType, Dev extends DeviceType = DeviceType>(input: Tensor<S, D, Dev>): Tensor<S, D, Dev>Element-wise bitwise NOT (complement): inverts all bits of integers.
Flips every bit: 0 becomes 1, 1 becomes 0. Also called one's complement. Essential for:
- Bit inversion: Flip all bits in a value
- Complement masks: Create inverse of a mask
- Negation: Subtract 1 from inverted value gives two's complement (negation)
- Feature flags: Disable all flags at once
- Logical negation: Invert boolean flags stored as bits
- Mask creation: Create mask by inverting existing mask
- Bit level NOT: Logical NOT at bitwise level
Binary behavior: Every 0 becomes 1, every 1 becomes 0.
Integer-only: Works with int32, int8, uint8, uint32, etc. Not valid for floats.
Signed integers: Results depend on representation (two's complement on most systems).
- Integer tensors only: Must use int8, uint8, int32, uint32 dtypes
- Unary operation: Only takes input, no second operand
- Self-inverse: bitwise_not(bitwise_not(x)) = x
- Two's complement: For signed ints, NOT x = -(x+1)
- Symmetric around -1: NOT distributes symmetrically around -1
- Single-argument: Works on any shape, preserves shape
- Integer-only: Cannot use with float32 or float64
- Signed/unsigned effects: Behavior differs for signed vs unsigned
- No identity element: NOT x != x for any x
- Bitwise vs logical: This is bitwise NOT, not logical !operator
Parameters
inputTensor<S, D, Dev>- Integer tensor (any shape)
Returns
Tensor<S, D, Dev>– Integer tensor with same shape; all bits invertedExamples
// Basic bitwise NOT
const x = torch.tensor([0, 1, 2, -1], { dtype: 'int32' });
torch.bitwise_not(x);
// [-1, -2, -3, 0]
// Shows two's complement relationship: NOT x = -(x + 1)// Inverting a mask: complement for filtering
const valid_mask = torch.tensor([0b11110000], { dtype: 'uint8' }); // Top 4 bits valid
const invalid_mask = torch.bitwise_not(valid_mask);
// [0b00001111] - bottom 4 bits (inverse set)// Disabling all features at once
const features = torch.tensor([0b11010101], { dtype: 'uint8' }); // Some on, some off
const all_off = torch.bitwise_not(features);
// [0b00101010] - inverted (off becomes on, on becomes off)// Creating complement for conditional logic
const permission = torch.tensor([0b11111111], { dtype: 'uint8' }); // All allowed
const restricted = torch.bitwise_not(permission);
// [0b00000000] - nothing allowed (complement)// Converting between signed/unsigned representations
const unsigned = torch.tensor([255, 128, 1], { dtype: 'uint8' });
const inverted = torch.bitwise_not(unsigned);
// In two's complement, related to negative numbersSee Also
- PyTorch torch.bitwise_not(input)
- bitwise_and - AND: result 1 where both inputs are 1
- bitwise_or - OR: result 1 where either input is 1
- bitwise_xor - XOR: result 1 where inputs differ
- bitwise_left_shift - Left shift: multiply by powers of 2
- bitwise_right_shift - Right shift: divide by powers of 2