Lua:bShr

From Cheat Engine
Revision as of 14:53, 3 December 2025 by Leunsel (talk | contribs) (Major overhaul of the post.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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[edit]

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[edit]

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

Description[edit]

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[edit]

Simple right shift:

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

Equivalent Lua operator:

print( 12 >> 2 )       -- 3

Extracting high nibble of a byte:

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

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

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

Behavior[edit]

  • 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[edit]

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))

See Also[edit]

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