Last active
June 23, 2017 23:05
-
-
Save connorjan/96dc1d1b9e606a4a1fbba7c26d0a523b to your computer and use it in GitHub Desktop.
Common python functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def expand(lst): | |
"""" | |
Expand a list/tuple of lists/tuples of lists/tuples of ... of values into a single list | |
""" | |
exp = [] | |
for item in lst: | |
if type(item) is list or type(item) is tuple: | |
exp += expand(item) | |
else: | |
exp.append(item) | |
return exp | |
def sliceBits(num, msb, lsb=None): | |
""" | |
Slice bits from an integer from the msb to the lsb (inclusive) and 0-indexed | |
If no lsb is provided it will only grab the one bit at the msb specified | |
num - the number | |
msb - the most significant bit to keep | |
lsb - the least significant bit to keep | |
ex. SliceBits(0xF000, 15, 12) = 0xF | |
ex. SliceBits(0x7800, 14, 11) = 0xF | |
""" | |
width = (msb-lsb+1) if lsb is not None else 1 | |
return (((2**(width))-1 << msb-width+1) & num) >> msb-width+1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment