From 8a1802ad9bba22e0319c8ffe3b5caaeb1d5364ed Mon Sep 17 00:00:00 2001 From: CPunch Date: Wed, 10 Aug 2022 17:39:51 -0500 Subject: [PATCH] better _get_bits() --- luac.py | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/luac.py b/luac.py index 6b58b54..eeee7f7 100644 --- a/luac.py +++ b/luac.py @@ -115,25 +115,8 @@ instr_lookup_tbl = [ ] # at [p]osition, with [s]ize of bits -def _get_bits(num, p, s): - # convert number into binary first - binary = bin(num) - - # remove first two characters - binary = binary[2:] - - # fill in missing bits - for i in range(32 - len(binary)): - binary = '0' + binary - - start = len(binary) - (p+s) - end = len(binary) - p - - # extract k bit sub-string - kBitSubStr = binary[start : end] - - # convert extracted sub-string into decimal again - return (int(kBitSubStr,2)) +def _get_bits(num: int, p: int, s: int): + return (num>>p) & (~((~0)<