Difference between revisions of "Lua:bShl"

From Cheat Engine
Jump to navigation Jump to search
(Major overhaul of the post.)
 
Line 1: Line 1:
<h2>bShl(int, int2)   : Binary shift left</h2>
+
[[Category:Lua]]
 +
'''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 (<code><<</code>) in Cheat Engine.
 +
 
 +
=== Function Parameters ===
 +
{|width="85%" cellpadding="10%" cellpadding="5%" cellspacing="0" border="0"
 +
!align="left"|Parameter
 +
!align="left"|Type
 +
!style="width: 80%;" align="left"|Description
 +
|-
 +
|int1
 +
|integer
 +
|The value whose bits will be shifted to the left.
 +
|-
 +
|int2
 +
|integer
 +
|The number of bit positions to shift <code>int1</code>.
 +
|}
 +
 
 +
=== 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):'''
 +
 
 +
<pre>
 +
0000 0011  (3)
 +
<< 2
 +
--------------
 +
0000 1100  (12)
 +
</pre>
 +
 
 +
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:'''
 +
 
 
<pre>
 
<pre>
-- 1 << 1
+
print( bShl(3, 2) )   -- 12
print( bShl(1,1) ) -- 2
+
</pre>
or
 
print(1 << 1)
 
  
0001
+
'''Equivalent Lua operator:'''
-----
+
 
0010
+
<pre>
 +
print( 3 << 2 )        -- 12
 
</pre>
 
</pre>
 +
 +
'''Creating a flag mask dynamically:'''
 +
 +
<pre>
 +
local bitIndex = 5
 +
local mask = bShl(1, bitIndex)
 +
print(string.format("0x%X", mask))  -- 0x20
 +
</pre>
 +
 +
'''Fast multiplication by powers of two:'''
 +
 +
<pre>
 +
local x = 7
 +
local result = bShl(x, 3)  -- 7 * 2^3 = 56
 +
print(result)
 +
</pre>
 +
 +
=== Behavior ===
 +
 +
* Returns an integer.
 +
* Only works on numeric values.
 +
* Equivalent to Lua's native bitwise shift-left operator (<code><<</code>).
 +
* 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:'''
 +
 +
<pre>
 +
local r = 0x12
 +
local g = 0x34
 +
local packed = bOr( bShl(r, 8), g )
 +
print(string.format("0x%X", packed))  -- 0x1234
 +
</pre>
 +
 +
'''Left-shifting to prepare bit regions for further combination:'''
 +
 +
<pre>
 +
local section = 0x7
 +
local shifted = bShl(section, 4)  -- move to bits 4–7
 +
print(string.format("0x%X", shifted))
 +
</pre>
 +
 +
=== See Also ===
 +
 +
* [[Lua:bOr|bOr]] - Bitwise OR
 +
* [[Lua:bXor|bXor]] - Bitwise XOR
 +
* [[Lua:bAnd|bAnd]] - Bitwise AND
 +
* [[Lua:bShl|bShl]] - Bitwise Shift Left
 +
* [[Lua:bShr|bShr]] - Bitwise Shift Right
 +
* [[Lua:bNot|bNot]] - Bitwise NOT

Latest revision as of 14:46, 3 December 2025

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

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

An integer representing the result of left‑shifting int1 by int2 bits.

Description[edit]

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

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

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

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

See Also[edit]

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