xor
What is XOR?
XOR (exclusive OR) is a logical operation used in computer science and cryptography.
- It works bit by bit on two values.
- Rule:
0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0
In short: the result is 1
only if the two bits are different.
Example (in binary)
1 | 1010 (decimal 10) |
So 10 ^ 6 = 12
.
Why is XOR used in coding?
- Reversible operation: If you XOR something twice with the same value, you get the original back.
Example:
1 | x = 42 |
This makes XOR a common trick for simple encryption/obfuscation.
XOR with a Key
Instead of XORing with just a number (like 0x32
), you can use a key (a number, string, or sequence).
Example with a single-number key:
1 | message = "HELLO" |
Example with a multi-character key:
If the key is longer (like a word), you usually repeat it across the message:
1 | message = "HELLO" |
In Short
XOR is like a switch: same input = off (0), different input = on (1).
It’s reversible, which is why it’s used in encryption.
Using a key makes the XOR cipher stronger than just a fixed number.
- Title: xor
- Author: Depe
- Created at : 2025-09-14 16:51:51
- Updated at : 2025-09-14 17:01:36
- Link: https://depe.blog/2025/09/14/xor/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments