Difference between revisions of "Lua:bAnd"

From Cheat Engine
Jump to navigation Jump to search
m (Added CodeBox Template.)
m (Syntax Highlighting.)
 
Line 41: Line 41:
 
'''Simple bitwise AND:'''
 
'''Simple bitwise AND:'''
  
<pre>
+
<syntaxhighlight lang="lua" line>
 
print( bAnd(13, 5) )  -- 5
 
print( bAnd(13, 5) )  -- 5
</pre>
+
</syntaxhighlight>
  
 
'''Using Lua's AND operator (equivalent):'''
 
'''Using Lua's AND operator (equivalent):'''
  
<pre>
+
<syntaxhighlight lang="lua" line>
 
print( 13 & 5 )        -- 5
 
print( 13 & 5 )        -- 5
</pre>
+
</syntaxhighlight>
  
 
'''Checking if a specific flag is set:'''
 
'''Checking if a specific flag is set:'''
  
<pre>
+
<syntaxhighlight lang="lua" line>
 
local FLAGS = 0x0D  -- 1101
 
local FLAGS = 0x0D  -- 1101
 
local CHECK = 0x04  -- 0100
 
local CHECK = 0x04  -- 0100
Line 60: Line 60:
 
   print("Flag is set!")
 
   print("Flag is set!")
 
end
 
end
</pre>
+
</syntaxhighlight>
  
 
'''Clearing bits using AND with a mask:'''
 
'''Clearing bits using AND with a mask:'''
  
<pre>
+
<syntaxhighlight lang="lua" line>
 
local value = 0x0F      -- 1111
 
local value = 0x0F      -- 1111
 
local mask  = 0xF0      -- clears lower bits
 
local mask  = 0xF0      -- clears lower bits
 
local result = bAnd(value, mask)
 
local result = bAnd(value, mask)
 
print(string.format("0x%X", result))
 
print(string.format("0x%X", result))
</pre>
+
</syntaxhighlight>
  
 
=== Behavior ===
 
=== Behavior ===
Line 81: Line 81:
 
'''Extracting a subset of bits (bitmask extraction):'''
 
'''Extracting a subset of bits (bitmask extraction):'''
  
<pre>
+
<syntaxhighlight lang="lua" line>
 
local value = 0x3A  -- 00111010
 
local value = 0x3A  -- 00111010
 
local mask  = 0x0F  -- 00001111
 
local mask  = 0x0F  -- 00001111
 
local lowerNibble = bAnd(value, mask)
 
local lowerNibble = bAnd(value, mask)
 
print(string.format("Lower nibble: %X", lowerNibble))
 
print(string.format("Lower nibble: %X", lowerNibble))
</pre>
+
</syntaxhighlight>
  
 
'''Checking multiple flags:'''
 
'''Checking multiple flags:'''
  
<pre>
+
<syntaxhighlight lang="lua" line>
 
local flags = 0x37  -- 110111
 
local flags = 0x37  -- 110111
 
local needed = 0x21  -- 100001
 
local needed = 0x21  -- 100001
Line 97: Line 97:
 
   print("All required flags are set")
 
   print("All required flags are set")
 
end
 
end
</pre>
+
</syntaxhighlight>
  
 
{{LuaSeeAlso}}
 
{{LuaSeeAlso}}
  
 
{{BinaryOps}}
 
{{BinaryOps}}

Latest revision as of 20:02, 25 June 2026

<> Reference

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.

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:

1 print( bAnd(13, 5) )   -- 5

Using Lua's AND operator (equivalent):

1 print( 13 & 5 )        -- 5

Checking if a specific flag is set:

1 local FLAGS = 0x0D   -- 1101
2 local CHECK = 0x04   -- 0100
3 
4 if bAnd(FLAGS, CHECK) ~= 0 then
5   print("Flag is set!")
6 end

Clearing bits using AND with a mask:

1 local value = 0x0F      -- 1111
2 local mask  = 0xF0      -- clears lower bits
3 local result = bAnd(value, mask)
4 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):

1 local value = 0x3A   -- 00111010
2 local mask  = 0x0F   -- 00001111
3 local lowerNibble = bAnd(value, mask)
4 print(string.format("Lower nibble: %X", lowerNibble))

Checking multiple flags:

1 local flags = 0x37   -- 110111
2 local needed = 0x21  -- 100001
3 
4 if bAnd(flags, needed) == needed then
5   print("All required flags are set")
6 end

See Also[edit]

Main Pages

Bitwise Related Lua Functions

bOr – Bitwise OR
bXor – Bitwise XOR
bAnd – Bitwise AND
bShl – Bitwise Shift Left
bShr – Bitwise Shift Right
bNot – Bitwise NOT