First, forgive me my ignorance to ask this kind of question, but I couldn't help as I am not good at Python.
I met following Python code that I need to convert into C++.
def getSignedNumber(number, bitLength):
mask = pow(2,bitLength) - 1
if number & (1 << (bitLength - 1)):
return number | ~mask
else:
return number & mask
def getUnsignedNumber(number, bitLength):
mask = pow(2,bitLength) - 1
return number & mask
This is used to convert unsigned int into signed int and vice versa. But I don't know why we have to do this because I think just casting to corresponding type would be enough. Maybe it's because Python is a dynamic-type language and needs this kind of operations? Am I missing something here?