Created
March 20, 2016 00:53
-
-
Save thequbit/1ddf4980ddce30ece9f9 to your computer and use it in GitHub Desktop.
This file contains 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 u8s_to_i16(low_u8, high_u8): | |
# get the high byte without the top bit ( sign bit ) | |
high_byte = high_u8 & 127 | |
# get the sign bit as a 1 ( negative ) or a 0 ( possitive ) | |
high_byte_sign = (high_u8 & 128) >> 7 # get top bit | |
# get the low byte ( no adjustments needed ) | |
low_byte = low_u8 | |
# generate -1 for negative, or 1 for possitive | |
sign = -1 if high_byte_sign == 1 else 1 | |
# shift the high byte up by 8 bits, add the low byte, and multiply by the sign bit | |
i16 = sign * ( (high_byte * 256) + low_byte ) | |
return i16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment