Difference between revisions of "Lua:bXor"
Jump to navigation
Jump to search
m (Added CodeBox Template.) |
m (Syntax Highlighting.) |
||
| Line 37: | Line 37: | ||
'''Simple bitwise XOR:''' | '''Simple bitwise XOR:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
print( bXor(3, 5) ) -- 6 | print( bXor(3, 5) ) -- 6 | ||
| − | </ | + | </syntaxhighlight> |
'''Using Lua's XOR operator (equivalent):''' | '''Using Lua's XOR operator (equivalent):''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
print( 3 ~ 5 ) -- 6 | print( 3 ~ 5 ) -- 6 | ||
| − | </ | + | </syntaxhighlight> |
'''Toggling flags with XOR:''' | '''Toggling flags with XOR:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local FLAG = 0x04 | local FLAG = 0x04 | ||
local value = 0x04 | local value = 0x04 | ||
| Line 56: | Line 56: | ||
toggled = bXor(value, FLAG) | toggled = bXor(value, FLAG) | ||
print(string.format("0x%X", toggled)) | print(string.format("0x%X", toggled)) | ||
| − | </ | + | </syntaxhighlight> |
'''Detecting bit differences:''' | '''Detecting bit differences:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local a = 0x0F | local a = 0x0F | ||
local b = 0x33 | local b = 0x33 | ||
local diff = bXor(a, b) | local diff = bXor(a, b) | ||
print(string.format("Different bits: %X", diff)) | print(string.format("Different bits: %X", diff)) | ||
| − | </ | + | </syntaxhighlight> |
=== Behavior === | === Behavior === | ||
| Line 77: | Line 77: | ||
'''Clearing bits using XOR (when bit is known to be set):''' | '''Clearing bits using XOR (when bit is known to be set):''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local value = 0x0A -- 1010 | local value = 0x0A -- 1010 | ||
local clearBit = 0x02 -- bit 1 | local clearBit = 0x02 -- bit 1 | ||
value = bXor(value, clearBit) | value = bXor(value, clearBit) | ||
print(string.format("0x%X", value)) | print(string.format("0x%X", value)) | ||
| − | </ | + | </syntaxhighlight> |
'''XOR-based encryption/decryption (simple):''' | '''XOR-based encryption/decryption (simple):''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local key = 0x55 | local key = 0x55 | ||
local original = 0x22 | local original = 0x22 | ||
| Line 94: | Line 94: | ||
print(encrypted, decrypted) | print(encrypted, decrypted) | ||
| − | </ | + | </syntaxhighlight> |
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
{{BinaryOps}} | {{BinaryOps}} | ||
Latest revision as of 20:02, 25 June 2026
Performs a bitwise XOR (exclusive OR) operation between two integer values. This function behaves the same as Lua's bitwise XOR operator (~) in Cheat Engine. It is used when toggling bits, detecting bit differences, or implementing specific bit-logic operations.
Contents
Function Parameters[edit]
| Parameter | Type | Description |
|---|---|---|
| int1 | integer | The first integer value used in the XOR operation. |
| int2 | integer | The second integer value used in the XOR operation. |
Returns[edit]
An integer representing the result of the bitwise XOR operation.
Description[edit]
The XOR operation compares each bit of the two integers and sets the result bit to 1 only if the bits differ. If both bits are the same (both 0 or both 1), the result bit is 0.
Binary example:
0011 (3) 0101 (5) ---- 0110 (6)
Usage Examples[edit]
Simple bitwise XOR:
1 print( bXor(3, 5) ) -- 6
Using Lua's XOR operator (equivalent):
1 print( 3 ~ 5 ) -- 6
Toggling flags with XOR:
1 local FLAG = 0x04
2 local value = 0x04
3
4 -- Toggle bit 2
5 toggled = bXor(value, FLAG)
6 print(string.format("0x%X", toggled))
Detecting bit differences:
1 local a = 0x0F
2 local b = 0x33
3 local diff = bXor(a, b)
4 print(string.format("Different bits: %X", diff))
Behavior[edit]
- Returns an integer.
- Only works on numeric values.
- Equivalent to Lua's native XOR operator (
~) in Cheat Engine. - Useful for flag toggling and detecting changed bits.
Advanced Examples[edit]
Clearing bits using XOR (when bit is known to be set):
1 local value = 0x0A -- 1010
2 local clearBit = 0x02 -- bit 1
3 value = bXor(value, clearBit)
4 print(string.format("0x%X", value))
XOR-based encryption/decryption (simple):
1 local key = 0x55
2 local original = 0x22
3
4 local encrypted = bXor(original, key)
5 local decrypted = bXor(encrypted, key)
6
7 print(encrypted, decrypted)