Skip to content

Commit 8a1802a

Browse files
committed
better _get_bits()
1 parent 3eecd3a commit 8a1802a

File tree

1 file changed

+2
-19
lines changed

1 file changed

+2
-19
lines changed

luac.py

+2-19
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,8 @@ def print(self):
115115
]
116116

117117
# at [p]osition, with [s]ize of bits
118-
def _get_bits(num, p, s):
119-
# convert number into binary first
120-
binary = bin(num)
121-
122-
# remove first two characters
123-
binary = binary[2:]
124-
125-
# fill in missing bits
126-
for i in range(32 - len(binary)):
127-
binary = '0' + binary
128-
129-
start = len(binary) - (p+s)
130-
end = len(binary) - p
131-
132-
# extract k bit sub-string
133-
kBitSubStr = binary[start : end]
134-
135-
# convert extracted sub-string into decimal again
136-
return (int(kBitSubStr,2))
118+
def _get_bits(num: int, p: int, s: int):
119+
return (num>>p) & (~((~0)<<s))
137120

138121
class LuaUndump:
139122
def __init__(self):

0 commit comments

Comments
 (0)