Difference between revisions of "Lua:bShl"
Jump to navigation
Jump to search
m (Added CodeBox Template.) |
m (Syntax Highlighting.) |
||
| Line 43: | Line 43: | ||
'''Simple left shift:''' | '''Simple left shift:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
print( bShl(3, 2) ) -- 12 | print( bShl(3, 2) ) -- 12 | ||
| − | </ | + | </syntaxhighlight> |
'''Equivalent Lua operator:''' | '''Equivalent Lua operator:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
print( 3 << 2 ) -- 12 | print( 3 << 2 ) -- 12 | ||
| − | </ | + | </syntaxhighlight> |
'''Creating a flag mask dynamically:''' | '''Creating a flag mask dynamically:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local bitIndex = 5 | local bitIndex = 5 | ||
local mask = bShl(1, bitIndex) | local mask = bShl(1, bitIndex) | ||
print(string.format("0x%X", mask)) -- 0x20 | print(string.format("0x%X", mask)) -- 0x20 | ||
| − | </ | + | </syntaxhighlight> |
'''Fast multiplication by powers of two:''' | '''Fast multiplication by powers of two:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local x = 7 | local x = 7 | ||
local result = bShl(x, 3) -- 7 * 2^3 = 56 | local result = bShl(x, 3) -- 7 * 2^3 = 56 | ||
print(result) | print(result) | ||
| − | </ | + | </syntaxhighlight> |
=== Behavior === | === Behavior === | ||
| Line 79: | Line 79: | ||
'''Packing two 8-bit values into a 16-bit integer:''' | '''Packing two 8-bit values into a 16-bit integer:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local r = 0x12 | local r = 0x12 | ||
local g = 0x34 | local g = 0x34 | ||
local packed = bOr( bShl(r, 8), g ) | local packed = bOr( bShl(r, 8), g ) | ||
print(string.format("0x%X", packed)) -- 0x1234 | print(string.format("0x%X", packed)) -- 0x1234 | ||
| − | </ | + | </syntaxhighlight> |
'''Left-shifting to prepare bit regions for further combination:''' | '''Left-shifting to prepare bit regions for further combination:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local section = 0x7 | local section = 0x7 | ||
local shifted = bShl(section, 4) -- move to bits 4–7 | local shifted = bShl(section, 4) -- move to bits 4–7 | ||
print(string.format("0x%X", shifted)) | print(string.format("0x%X", shifted)) | ||
| − | </ | + | </syntaxhighlight> |
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
{{BinaryOps}} | {{BinaryOps}} | ||
Latest revision as of 20:01, 25 June 2026
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[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:
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[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:
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))