I use a lambda function to convert a decimal negative number to binary two's complement. However the result is not a binary number.
My code:
num1 = int(raw_input("Enter first number"))
if num1 < 0:
def tobin(x, count = 8):
return "".join(map(lambda y:str((x>>y)&1), range(count-1, -1, -1)))
num1 = tobin
print num1
Result:
Enter first number -5
<function tobin at 0x1f9c260>
The expected result should be 1011.
num1 = int(raw_input("Enter first bumber:\t"))
def tobin(x, count=4):
return "".join(map(lambda y: str((x >> y) & 1), range(count - 1, -1, -1)))
if num1 < 0:
num1 = tobin(num1)
print num1
result:
Enter first bumber: -5
1011