Bit xor
BITXOR Function¶
The BITXOR function in Excel performs a bitwise XOR (exclusive OR) operation between the binary representations of
two numbers. The XOR operation compares each bit in the binary forms of the two numbers, and the result is 1 if the
bits are different and 0 if the bits are the same.
In simpler terms:
- If a bit in one number is
1and the corresponding bit in the other is0, the result bit will be1. - If both corresponding bits are the same (both
1or both0), the result bit will be0.
Key Features of BITXOR:¶
- Outputs the binary XOR operation between two integers.
- The result combines binary values from two numbers where differences in their bits determine
1, and similarities determine0. - Useful for cryptography, binary logic, and low-level data manipulation.
Syntax:¶
- number1: A non-negative integer.
- number2: A non-negative integer.
Examples:¶
-
Basic XOR Operation:
Binary
=BITXOR(5, 3)
Binary representation of5is0101, and3is0011.
XOR operation:0110is6in decimal.
Result:6 -
XOR with Zero:
=BITXOR(7, 0)
Binary representation of7is0111. XOR with zero leaves the number unchanged.
Result:7 -
XOR of Same Numbers:
=BITXOR(9, 9)
Binary representation of9is1001. XOR of any number with itself results in0because every bit is the same.
Result:0 -
XOR with Larger Numbers:
Binary
=BITXOR(15, 6)
Binary representation of15is1111, and6is0110.
XOR operation:1001is9in decimal.
Result:9
Notes:¶
-
Non-Negative Numbers Only:
- The inputs
number1andnumber2must be non-negative integers. Negative inputs will result in a#NUM!error.
- The inputs
-
Error for Non-Integers:
- If either
number1ornumber2is not an integer, Excel returns a#VALUE!error.
- If either
-
Behavior with XOR:
- XOR always clears (results in
0) when both bits are the same. - XOR is commutative:
BITXOR(a, b)equalsBITXOR(b, a).
- XOR always clears (results in
Applications:¶
-
Use Case:
- Useful for toggling bits or performing simple cryptographic operations.
- Ensures differentiation between changes in binary data.
-
Complementary Functions:
- BITAND: Performs a bitwise AND operation.
- BITOR: Performs a bitwise OR operation.
- BITLSHIFT: Shifts bits to the left by a specified number.
- BITRSHIFT: Shifts bits to the right by a specified number.