Difference between revisions of "Lua:bAnd"
Jump to navigation
Jump to search
(Major overhaul of the post.) |
|||
| Line 1: | Line 1: | ||
| − | + | [[Category:Lua]] | |
| + | '''function''' bAnd(''int1'', ''int2'') ''':''' Integer | ||
| + | |||
| + | Performs a bitwise AND operation between two integer values. This function behaves the same as Lua's bitwise AND operator (<code>&</code>) in Cheat Engine. It is commonly used to check whether specific bits are set or to filter bitmask values. | ||
| + | |||
| + | === 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 bitwise AND operation. | ||
| + | |- | ||
| + | |int2 | ||
| + | |integer | ||
| + | |The second integer value used in the bitwise AND operation. | ||
| + | |} | ||
| + | |||
| + | === Returns === | ||
| + | An integer representing the result of the bitwise AND operation. | ||
| + | |||
| + | === Description === | ||
| + | The bitwise AND operation compares each bit of the two integers and sets the bit in the result to '''1''' only if both bits are '''1'''. This is often used for: | ||
| + | |||
| + | * Checking whether a specific flag is enabled | ||
| + | * Filtering unwanted bits | ||
| + | * Performing masked comparisons | ||
| + | |||
| + | '''Binary example:''' | ||
| + | |||
| + | <pre> | ||
| + | 1101 (13) | ||
| + | 0101 (5) | ||
| + | ---- | ||
| + | 0101 (5) | ||
| + | </pre> | ||
| + | |||
| + | === Usage Examples === | ||
| + | '''Simple bitwise AND:''' | ||
| + | |||
| + | <pre> | ||
| + | print( bAnd(13, 5) ) -- 5 | ||
| + | </pre> | ||
| + | |||
| + | '''Using Lua's AND operator (equivalent):''' | ||
| + | |||
| + | <pre> | ||
| + | print( 13 & 5 ) -- 5 | ||
| + | </pre> | ||
| + | |||
| + | '''Checking if a specific flag is set:''' | ||
| + | |||
| + | <pre> | ||
| + | local FLAGS = 0x0D -- 1101 | ||
| + | local CHECK = 0x04 -- 0100 | ||
| + | |||
| + | if bAnd(FLAGS, CHECK) ~= 0 then | ||
| + | print("Flag is set!") | ||
| + | end | ||
| + | </pre> | ||
| + | |||
| + | '''Clearing bits using AND with a mask:''' | ||
| + | |||
| + | <pre> | ||
| + | local value = 0x0F -- 1111 | ||
| + | local mask = 0xF0 -- clears lower bits | ||
| + | local result = bAnd(value, mask) | ||
| + | print(string.format("0x%X", result)) | ||
| + | </pre> | ||
| + | |||
| + | === Behavior === | ||
| + | |||
| + | * Returns an integer value. | ||
| + | * Only works on numeric inputs. | ||
| + | * Equivalent to Lua's native bitwise AND operator (<code>&</code>) in Cheat Engine. | ||
| + | * Commonly used for feature flags, filtering, and masked logic. | ||
| + | |||
| + | === Advanced Examples === | ||
| + | '''Extracting a subset of bits (bitmask extraction):''' | ||
| + | |||
<pre> | <pre> | ||
| − | -- | + | local value = 0x3A -- 00111010 |
| − | + | local mask = 0x0F -- 00001111 | |
| − | + | local lowerNibble = bAnd(value, mask) | |
| − | print ( | + | print(string.format("Lower nibble: %X", lowerNibble)) |
| + | </pre> | ||
| − | + | '''Checking multiple flags:''' | |
| − | + | ||
| − | ---- | + | <pre> |
| − | + | local flags = 0x37 -- 110111 | |
| + | local needed = 0x21 -- 100001 | ||
| + | |||
| + | if bAnd(flags, needed) == needed then | ||
| + | print("All required flags are set") | ||
| + | end | ||
</pre> | </pre> | ||
| + | |||
| + | === See Also === | ||
| + | |||
| + | * [[Lua:bOr|bOr]] - Bitwise OR | ||
| + | * [[Lua:bXor|bXor]] - Bitwise XOR | ||
| + | * [[Lua:bAnd|bAnd]] - Bitwise AND | ||
| + | * [[Lua:bShl|bShl]] - Bitwise Shift Left | ||
| + | * [[Lua:bShr|bShr]] - Bitwise Shift Right | ||
| + | * [[Lua:bNot|bNot]] - Bitwise NOT | ||
Latest revision as of 14:46, 3 December 2025
function bAnd(int1, int2) : Integer
Performs a bitwise AND operation between two integer values. This function behaves the same as Lua's bitwise AND operator (&) in Cheat Engine. It is commonly used to check whether specific bits are set or to filter bitmask values.
Contents
Function Parameters[edit]
| Parameter | Type | Description |
|---|---|---|
| int1 | integer | The first integer value used in the bitwise AND operation. |
| int2 | integer | The second integer value used in the bitwise AND operation. |
Returns[edit]
An integer representing the result of the bitwise AND operation.
Description[edit]
The bitwise AND operation compares each bit of the two integers and sets the bit in the result to 1 only if both bits are 1. This is often used for:
- Checking whether a specific flag is enabled
- Filtering unwanted bits
- Performing masked comparisons
Binary example:
1101 (13) 0101 (5) ---- 0101 (5)
Usage Examples[edit]
Simple bitwise AND:
print( bAnd(13, 5) ) -- 5
Using Lua's AND operator (equivalent):
print( 13 & 5 ) -- 5
Checking if a specific flag is set:
local FLAGS = 0x0D -- 1101
local CHECK = 0x04 -- 0100
if bAnd(FLAGS, CHECK) ~= 0 then
print("Flag is set!")
end
Clearing bits using AND with a mask:
local value = 0x0F -- 1111
local mask = 0xF0 -- clears lower bits
local result = bAnd(value, mask)
print(string.format("0x%X", result))
Behavior[edit]
- Returns an integer value.
- Only works on numeric inputs.
- Equivalent to Lua's native bitwise AND operator (
&) in Cheat Engine. - Commonly used for feature flags, filtering, and masked logic.
Advanced Examples[edit]
Extracting a subset of bits (bitmask extraction):
local value = 0x3A -- 00111010
local mask = 0x0F -- 00001111
local lowerNibble = bAnd(value, mask)
print(string.format("Lower nibble: %X", lowerNibble))
Checking multiple flags:
local flags = 0x37 -- 110111
local needed = 0x21 -- 100001
if bAnd(flags, needed) == needed then
print("All required flags are set")
end