Difference between revisions of "Lua:bXor"
m (Added CodeBox Template.) |
|||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | + | [[Category:Lua]] | |
| + | {{CodeBox|'''function''' bXor(''int1'', ''int2'') ''':''' Integer}} | ||
| + | |||
| + | Performs a bitwise XOR (exclusive OR) operation between two integer values. This function behaves the same as Lua's bitwise XOR operator (<code>~</code>) in Cheat Engine. It is used when toggling bits, detecting bit differences, or implementing specific bit-logic operations. | ||
| + | |||
| + | === Function Parameters === | ||
| + | {|width="85%" cellpadding="10%" cellpadding="5%" cellspacing="0" border="0" | ||
| + | !align="left"|Parameter | ||
| + | !align="left"|Type | ||
| + | !style="width: 80%;" align="left"|Description | ||
| + | |- | ||
| + | |int1 | ||
| + | |integer | ||
| + | |The first integer value used in the XOR operation. | ||
| + | |- | ||
| + | |int2 | ||
| + | |integer | ||
| + | |The second integer value used in the XOR operation. | ||
| + | |} | ||
| + | |||
| + | === Returns === | ||
| + | An integer representing the result of the bitwise XOR operation. | ||
| + | |||
| + | === Description === | ||
| + | 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:''' | ||
| + | |||
| + | <pre> | ||
| + | 0011 (3) | ||
| + | 0101 (5) | ||
| + | ---- | ||
| + | 0110 (6) | ||
| + | </pre> | ||
| + | |||
| + | === Usage Examples === | ||
| + | '''Simple bitwise XOR:''' | ||
| + | |||
| + | <pre> | ||
| + | print( bXor(3, 5) ) -- 6 | ||
| + | </pre> | ||
| + | |||
| + | '''Using Lua's XOR operator (equivalent):''' | ||
| + | |||
| + | <pre> | ||
| + | print( 3 ~ 5 ) -- 6 | ||
| + | </pre> | ||
| + | |||
| + | '''Toggling flags with XOR:''' | ||
| + | |||
| + | <pre> | ||
| + | local FLAG = 0x04 | ||
| + | local value = 0x04 | ||
| + | |||
| + | -- Toggle bit 2 | ||
| + | toggled = bXor(value, FLAG) | ||
| + | print(string.format("0x%X", toggled)) | ||
| + | </pre> | ||
| + | |||
| + | '''Detecting bit differences:''' | ||
| + | |||
| + | <pre> | ||
| + | local a = 0x0F | ||
| + | local b = 0x33 | ||
| + | local diff = bXor(a, b) | ||
| + | print(string.format("Different bits: %X", diff)) | ||
| + | </pre> | ||
| + | |||
| + | === Behavior === | ||
| + | |||
| + | * Returns an integer. | ||
| + | * Only works on numeric values. | ||
| + | * Equivalent to Lua's native XOR operator (<code>~</code>) in Cheat Engine. | ||
| + | * Useful for flag toggling and detecting changed bits. | ||
| + | |||
| + | === Advanced Examples === | ||
| + | '''Clearing bits using XOR (when bit is known to be set):''' | ||
| + | |||
<pre> | <pre> | ||
| − | -- 1 | + | local value = 0x0A -- 1010 |
| − | + | local clearBit = 0x02 -- bit 1 | |
| − | + | value = bXor(value, clearBit) | |
| − | print ( | + | print(string.format("0x%X", value)) |
| + | </pre> | ||
| − | + | '''XOR-based encryption/decryption (simple):''' | |
| − | + | ||
| − | + | <pre> | |
| − | + | local key = 0x55 | |
| + | local original = 0x22 | ||
| + | |||
| + | local encrypted = bXor(original, key) | ||
| + | local decrypted = bXor(encrypted, key) | ||
| + | |||
| + | print(encrypted, decrypted) | ||
</pre> | </pre> | ||
| + | |||
| + | {{LuaSeeAlso}} | ||
| + | |||
| + | {{BinaryOps}} | ||
Latest revision as of 23:41, 4 December 2025
| <> Function function bXor(int1, int2) : Integer |
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:
print( bXor(3, 5) ) -- 6
Using Lua's XOR operator (equivalent):
print( 3 ~ 5 ) -- 6
Toggling flags with XOR:
local FLAG = 0x04
local value = 0x04
-- Toggle bit 2
toggled = bXor(value, FLAG)
print(string.format("0x%X", toggled))
Detecting bit differences:
local a = 0x0F
local b = 0x33
local diff = bXor(a, b)
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):
local value = 0x0A -- 1010
local clearBit = 0x02 -- bit 1
value = bXor(value, clearBit)
print(string.format("0x%X", value))
XOR-based encryption/decryption (simple):
local key = 0x55 local original = 0x22 local encrypted = bXor(original, key) local decrypted = bXor(encrypted, key) print(encrypted, decrypted)
See also[edit]
| Lua |
| Script Engine |
Related Functions[edit]
| bOr – Bitwise OR |
| bXor – Bitwise XOR |
| bAnd – Bitwise AND |
| bShl – Bitwise Shift Left |
| bShr – Bitwise Shift Right |
| bNot – Bitwise NOT |