Lua:bShl

From Cheat Engine
Revision as of 20:01, 25 June 2026 by Leunsel (talk | contribs) (Syntax Highlighting.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

<> Lua API Reference

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.

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:

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

Equivalent Lua operator:

1 print( 3 << 2 )        -- 12

Creating a flag mask dynamically:

1 local bitIndex = 5
2 local mask = bShl(1, bitIndex)
3 print(string.format("0x%X", mask))  -- 0x20

Fast multiplication by powers of two:

1 local x = 7
2 local result = bShl(x, 3)  -- 7 * 2^3 = 56
3 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:

1 local r = 0x12
2 local g = 0x34
3 local packed = bOr( bShl(r, 8), g )
4 print(string.format("0x%X", packed))  -- 0x1234

Left-shifting to prepare bit regions for further combination:

1 local section = 0x7
2 local shifted = bShl(section, 4)  -- move to bits 4–7
3 print(string.format("0x%X", shifted))

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