Lua:bShl
function bShl(int1, int2) : Integer
Performs a bitwise left shift operation. This function shifts the bits of the first integer (int1) to the left by the number of positions specified in int2. It behaves the same as Lua’s left‑shift operator (<<) in Cheat Engine.
Contents
Function Parameters
| Parameter | Type | Description |
|---|---|---|
| int1 | integer | The value whose bits will be shifted to the left. |
| int2 | integer | The number of bit positions to shift int1.
|
Returns
An integer representing the result of left‑shifting int1 by int2 bits.
Description
Bit shifting left moves every bit of the number toward the more significant side. Each shift effectively multiplies the value by 2.
Binary example (3 << 2):
0000 0011 (3) << 2 -------------- 0000 1100 (12)
Left shifting is commonly used for:
- Creating or manipulating bitmasks
- Efficient multiplication by powers of two
- Packing values into bit fields
Usage Examples
Simple left shift:
print( bShl(3, 2) ) -- 12
Equivalent Lua operator:
print( 3 << 2 ) -- 12
Creating a flag mask dynamically:
local bitIndex = 5
local mask = bShl(1, bitIndex)
print(string.format("0x%X", mask)) -- 0x20
Fast multiplication by powers of two:
local x = 7 local result = bShl(x, 3) -- 7 * 2^3 = 56 print(result)
Behavior
- Returns an integer.
- Only works on numeric values.
- Equivalent to Lua's native bitwise shift-left operator (
<<). - Bits shifted out on the left are discarded; zeroes are shifted in from the right.
Advanced Examples
Packing two 8-bit values into a 16-bit integer:
local r = 0x12
local g = 0x34
local packed = bOr( bShl(r, 8), g )
print(string.format("0x%X", packed)) -- 0x1234
Left-shifting to prepare bit regions for further combination:
local section = 0x7
local shifted = bShl(section, 4) -- move to bits 4–7
print(string.format("0x%X", shifted))