Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to bitwise XOR of hex numbers in Python?
The bitwise XOR is a binary operation that compares two binary numbers bit by bit and returns "1" if the bits are different, and "0" if they are the same. The XOR (exclusive OR) operation follows these rules ?
| A | B | A ? B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Hexadecimal is a numbering system that uses a base-16 representation for numeric values. This system has 16 symbols: 0-9 and A-F, where A=10, B=11, C=12, D=13, E=14, F=15.
Using the XOR Operator (^)
To perform bitwise XOR on hex numbers in Python, use the XOR operator (^). The 0x prefix indicates hexadecimal representation ?
# Hexadecimal numbers
a = 0x12ef # 4847 in decimal
b = 0xabcd # 43981 in decimal
# Perform XOR operation
result = a ^ b
print(f"0x{a:x} ^ 0x{b:x} = 0x{result:x}")
print(f"Decimal: {a} ^ {b} = {result}")
0x12ef ^ 0xabcd = 0xb922 Decimal: 4847 ^ 43981 = 47398
Step-by-Step Binary XOR
Let's see how the XOR operation works at the binary level ?
a = 0x12ef
b = 0xabcd
print(f"a = 0x{a:x} = {bin(a)}")
print(f"b = 0x{b:x} = {bin(b)}")
print(f"XOR result = 0x{a^b:x} = {bin(a^b)}")
# Show bit-by-bit comparison
print("\nBit-by-bit XOR:")
print(f" {a:016b} (a)")
print(f"^ {b:016b} (b)")
print(f" {'-'*16}")
print(f" {a^b:016b} (result)")
a = 0x12ef = 0b1001011101111 b = 0xabcd = 0b10101011110011101 XOR result = 0xb922 = 0b10111001001000010 Bit-by-bit XOR: 0001001011101111 (a) ^ 1010101111001101 (b) ---------------- 1011100100100010 (result)
XOR with Multiple Hex Numbers
You can chain multiple XOR operations together ?
hex_numbers = [0x12ef, 0xabcd, 0x5a5a, 0xffff]
result = 0
# XOR all numbers together
for num in hex_numbers:
result ^= num
print(f"After XOR with 0x{num:x}: 0x{result:x}")
print(f"\nFinal result: 0x{result:x} ({result} in decimal)")
After XOR with 0x12ef: 0x12ef After XOR with 0xabcd: 0xb922 After XOR with 0x5a5a: 0xe378 After XOR with 0xffff: 0x1c87 Final result: 0x1c87 (7303 in decimal)
Converting Between Hex and Binary
Here's how to work with different number formats ?
# Different ways to represent hex numbers
hex_string = "A1B2"
hex_int = int(hex_string, 16) # Convert hex string to integer
hex_literal = 0xC3D4
print(f"Hex string: {hex_string}")
print(f"As integer: {hex_int}")
print(f"Hex literal: 0x{hex_literal:x}")
# XOR operations
result1 = hex_int ^ hex_literal
print(f"\nXOR result: 0x{result1:x}")
print(f"As hex string: {hex(result1)}")
print(f"As binary: {bin(result1)}")
Hex string: A1B2 As integer: 41394 Hex literal: 0xc3d4 XOR result: 0x6266 As hex string: 0x6266 As binary: 0b110001001100110
Conclusion
Use the XOR operator (^) to perform bitwise XOR on hexadecimal numbers in Python. The operation works at the binary level, comparing each bit position and returning 1 for different bits and 0 for same bits. Python handles the conversion between hex and binary automatically.
