Difference between revisions of "Lua:bShr"
Jump to navigation
Jump to search
(Major overhaul of the post.) |
|||
| Line 1: | Line 1: | ||
| − | + | [[Category:Lua]] | |
| + | '''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 (<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 right. | ||
| + | |- | ||
| + | |int2 | ||
| + | |integer | ||
| + | |The number of bit positions to shift <code>int1</code>. | ||
| + | |} | ||
| + | |||
| + | === Returns === | ||
| + | An integer representing the result of right-shifting ''int1'' by ''int2'' bits. | ||
| + | |||
| + | === Description === | ||
| + | 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):''' | ||
| + | |||
| + | <pre> | ||
| + | 0000 1100 (12) | ||
| + | >> 2 | ||
| + | -------------- | ||
| + | 0000 0011 (3) | ||
| + | </pre> | ||
| + | |||
| + | Right shifting is commonly used for: | ||
| + | |||
| + | * Extracting specific bit fields | ||
| + | * Efficient division by powers of two | ||
| + | * Unpacking packed data | ||
| + | |||
| + | === Usage Examples === | ||
| + | '''Simple right shift:''' | ||
| + | |||
<pre> | <pre> | ||
| − | + | print( bShr(12, 2) ) -- 3 | |
| − | print( bShr( | + | </pre> |
| − | + | ||
| − | + | '''Equivalent Lua operator:''' | |
| − | + | <pre> | |
| − | -- | + | print( 12 >> 2 ) -- 3 |
| − | |||
</pre> | </pre> | ||
| + | |||
| + | '''Extracting high nibble of a byte:''' | ||
| + | |||
| + | <pre> | ||
| + | local byte = 0xAB -- 10101011 | ||
| + | local highNibble = bShr(byte, 4) -- 0xA | ||
| + | print(string.format("0x%X", highNibble)) | ||
| + | </pre> | ||
| + | |||
| + | '''Unpacking a 16-bit value into two 8-bit values:''' | ||
| + | |||
| + | <pre> | ||
| + | local packed = 0x1234 | ||
| + | local high = bShr(packed, 8) & 0xFF | ||
| + | local low = packed & 0xFF | ||
| + | print(string.format("High: 0x%X, Low: 0x%X", high, low)) | ||
| + | </pre> | ||
| + | |||
| + | === Behavior === | ||
| + | |||
| + | * 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 (<code>>></code> in CE Lua syntax). | ||
| + | |||
| + | === Advanced Examples === | ||
| + | '''Combining shift with OR to manipulate bit fields:''' | ||
| + | |||
| + | <pre> | ||
| + | local section = 0xF0 | ||
| + | local shifted = bShr(section, 4) -- move to bits 0–3 | ||
| + | local result = bOr(shifted, 0x05) | ||
| + | print(string.format("0x%X", result)) | ||
| + | </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:53, 3 December 2025
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.
Contents
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))