Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Python是一个负数函数_Python 2.7_Bit Manipulation_Twos Complement - Fatal编程技术网

Python 2.7 Python是一个负数函数

Python 2.7 Python是一个负数函数,python-2.7,bit-manipulation,twos-complement,Python 2.7,Bit Manipulation,Twos Complement,我使用lamba函数将十进制负数转换为二进制2的补码,但结果不是二进制数 我的代码: num1 = int(raw_input("Enter first bumber")) 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 num1=int

我使用lamba函数将十进制负数转换为二进制2的补码,但结果不是二进制数

我的代码:

num1 = int(raw_input("Enter first bumber"))

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
num1=int(原始输入(“输入第一个bumber”))
如果num1<0:
def托宾(x,计数=8):
返回“”。连接(映射(lambda y:str((x>>y)&1),范围(count-1,-1,-1)))
num1=托宾
打印num1
结果:

Enter first bumber -5
<function tobin at 0x1f9c260>
输入第一个bumber-5
预期结果应为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
Enter first bumber: -5
1011