Difference between revisions of "Lua:bShr"
Jump to navigation
Jump to search
m (Added CodeBox Template.) |
(→Advanced Examples) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 43: | Line 43: | ||
'''Simple right shift:''' | '''Simple right shift:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
print( bShr(12, 2) ) -- 3 | print( bShr(12, 2) ) -- 3 | ||
| − | </ | + | </syntaxhighlight> |
'''Equivalent Lua operator:''' | '''Equivalent Lua operator:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
print( 12 >> 2 ) -- 3 | print( 12 >> 2 ) -- 3 | ||
| − | </ | + | </syntaxhighlight> |
'''Extracting high nibble of a byte:''' | '''Extracting high nibble of a byte:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local byte = 0xAB -- 10101011 | local byte = 0xAB -- 10101011 | ||
local highNibble = bShr(byte, 4) -- 0xA | local highNibble = bShr(byte, 4) -- 0xA | ||
print(string.format("0x%X", highNibble)) | print(string.format("0x%X", highNibble)) | ||
| − | </ | + | </syntaxhighlight> |
'''Unpacking a 16-bit value into two 8-bit values:''' | '''Unpacking a 16-bit value into two 8-bit values:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local packed = 0x1234 | local packed = 0x1234 | ||
local high = bShr(packed, 8) & 0xFF | local high = bShr(packed, 8) & 0xFF | ||
local low = packed & 0xFF | local low = packed & 0xFF | ||
print(string.format("High: 0x%X, Low: 0x%X", high, low)) | print(string.format("High: 0x%X, Low: 0x%X", high, low)) | ||
| − | </ | + | </syntaxhighlight> |
=== Behavior === | === Behavior === | ||
| Line 80: | Line 80: | ||
'''Combining shift with OR to manipulate bit fields:''' | '''Combining shift with OR to manipulate bit fields:''' | ||
| − | < | + | <syntaxhighlight lang="lua" line> |
local section = 0xF0 | local section = 0xF0 | ||
local shifted = bShr(section, 4) -- move to bits 0–3 | local shifted = bShr(section, 4) -- move to bits 0–3 | ||
local result = bOr(shifted, 0x05) | local result = bOr(shifted, 0x05) | ||
print(string.format("0x%X", result)) | print(string.format("0x%X", result)) | ||
| − | </ | + | </syntaxhighlight> |
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
{{BinaryOps}} | {{BinaryOps}} | ||
Latest revision as of 20:00, 25 June 2026
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:
1 print( bShr(12, 2) ) -- 3
Equivalent Lua operator:
1 print( 12 >> 2 ) -- 3
Extracting high nibble of a byte:
1 local byte = 0xAB -- 10101011
2 local highNibble = bShr(byte, 4) -- 0xA
3 print(string.format("0x%X", highNibble))
Unpacking a 16-bit value into two 8-bit values:
1 local packed = 0x1234
2 local high = bShr(packed, 8) & 0xFF
3 local low = packed & 0xFF
4 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:
1 local section = 0xF0
2 local shifted = bShr(section, 4) -- move to bits 0–3
3 local result = bOr(shifted, 0x05)
4 print(string.format("0x%X", result))