Python的Bitshift 32位约束 [英] Python Bitshift 32 Bit Constraint

查看:438
本文介绍了Python的Bitshift 32位约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2300740/problem-in-calculating-checksum-casting-int-to-signed-int32\">Problem在计算校验和:铸造诚信,以签订INT32

这应该是一个比较简单的答案,我只是真的不知道如何寻找它...我得到了一些半相关的<一个href=\"http://stackoverflow.com/questions/210629/python-unsigned-32-bit-bitwise-arithmetic\">things,但没有一个适合我想要做的。

This should be a relatively easy answer, I just don't really know how to search for it...I got a few semi-relevant things, but nothing that fits what I'm trying to do.

>>> 1171855803 << 7
149997542784L # I want -326312576

在换言之,处理的数量为整数且不允许它要转换为长。
我会怎么做呢?

In other words, treat the number as an integer and don't allow it to convert to a long. How would I do this?

我试着在<一个解决方案href=\"http://stackoverflow.com/questions/2300740/problem-in-calculating-checksum-casting-int-to-signed-int32\">this问题:

>>> x = 0xFFFFFFFF & (1171855803 << 7)
>>> if x > 0x7FFFFFFF: print -(~(x - 1) & 0xFFFFFFFF)
else: print x

-326312576L # yay!

它的工作原理!

推荐答案

也许不是最好的答案,但这个工程...

May not be the best answer, but this works...

import numpy as np
np.int32(1171855803) << 7

这篇关于Python的Bitshift 32位约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆