Difference between revisions of "Lua:bShr"

From Cheat Engine
Jump to navigation Jump to search
m (Added CodeBox Template.)
m (Usage Examples)
Line 43: Line 43:
 
'''Simple right shift:'''
 
'''Simple right shift:'''
  
<pre>
+
<syntaxhighlight lang="lua" line>
 
print( bShr(12, 2) )  -- 3
 
print( bShr(12, 2) )  -- 3
</pre>
+
</syntaxhighlight>
  
 
'''Equivalent Lua operator:'''
 
'''Equivalent Lua operator:'''
  
<pre>
+
<syntaxhighlight lang="lua" line>
 
print( 12 >> 2 )      -- 3
 
print( 12 >> 2 )      -- 3
</pre>
+
</syntaxhighlight>
  
 
'''Extracting high nibble of a byte:'''
 
'''Extracting high nibble of a byte:'''
  
<pre>
+
<syntaxhighlight lang="lua" line>
 
local byte = 0xAB      -- 10101011
 
local byte = 0xAB      -- 10101011
 
local highNibble = bShr(byte, 4)  -- 0xA
 
local highNibble = bShr(byte, 4)  -- 0xA
 
print(string.format("0x%X", highNibble))
 
print(string.format("0x%X", highNibble))
</pre>
+
</syntaxhighlight>
  
 
'''Unpacking a 16-bit value into two 8-bit values:'''
 
'''Unpacking a 16-bit value into two 8-bit values:'''
  
<pre>
+
<syntaxhighlight lang="lua" line>
 
local packed = 0x1234
 
local packed = 0x1234
 
local high = bShr(packed, 8) & 0xFF
 
local high = bShr(packed, 8) & 0xFF
 
local low  = packed & 0xFF
 
local low  = packed & 0xFF
 
print(string.format("High: 0x%X, Low: 0x%X", high, low))
 
print(string.format("High: 0x%X, Low: 0x%X", high, low))
</pre>
+
</syntaxhighlight>
  
 
=== Behavior ===
 
=== Behavior ===

Revision as of 20:00, 25 June 2026

<> Lua API Reference

function bShr(int1, int2) : Integer

Performs a bitwise right shift operation. This function shifts the bits of the first integer (int1) to the right by the number of positions specified in int2. It behaves the same as Lua’s right-shift operator (>) in Cheat Engine.

Function Parameters

Parameter Type Description
int1 integer The value whose bits will be shifted to the right.
int2 integer The number of bit positions to shift int1.

Returns

An integer representing the result of right-shifting int1 by int2 bits.

Description

Bit shifting right moves every bit of the number toward the less significant side. Each shift effectively divides the value by 2, discarding any remainder.

Binary example (12 >> 2):

0000 1100   (12)
>> 2
--------------
0000 0011   (3)

Right shifting is commonly used for:

  • Extracting specific bit fields
  • Efficient division by powers of two
  • Unpacking packed data

Usage Examples

Simple right shift:

1 print( bShr(12, 2) )   -- 3

Equivalent Lua operator:

1 print( 12 >> 2 )       -- 3

Extracting high nibble of a byte:

1 local byte = 0xAB       -- 10101011
2 local highNibble = bShr(byte, 4)  -- 0xA
3 print(string.format("0x%X", highNibble))

Unpacking a 16-bit value into two 8-bit values:

1 local packed = 0x1234
2 local high = bShr(packed, 8) & 0xFF
3 local low  = packed & 0xFF
4 print(string.format("High: 0x%X, Low: 0x%X", high, low))

Behavior

  • Returns an integer.
  • Only works on numeric values.
  • Bits shifted out on the right are discarded; zeroes are shifted in from the left.
  • Equivalent to Lua's native bitwise shift-right operator (>> in CE Lua syntax).

Advanced Examples

Combining shift with OR to manipulate bit fields:

local section = 0xF0
local shifted = bShr(section, 4)  -- move to bits 0–3
local result = bOr(shifted, 0x05)
print(string.format("0x%X", result))

Main Pages

Core Lua documentation entry points

Lua
Script Engine

Bitwise Related Lua Functions

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