如何在Python中获取long的有符号整数值?
如果 lv 存储一个 long 值,并且机器是 32 位,则以下代码:
iv = int(lv & 0xffffffff)
结果是一个 long 类型的 iv,而不是机器的 int。
在这种情况下如何获得(有符号)int 值?
If lv stores a long value, and the machine is 32 bits, the following code:
iv = int(lv & 0xffffffff)
results an iv of type long, instead of the machine's int.
How can I get the (signed) int value in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
本质上,问题是将符号从 32 位扩展到……无限位数,因为 Python 具有任意大的整数。通常,符号扩展是在转换时由 CPU 指令自动完成的,因此有趣的是,这在 Python 中比在 C 语言中更难。
通过尝试,我发现了类似于 BreizhGatch 函数的功能,但这不需要条件语句。
n & 0x80000000
提取32位符号位;然后,-
保留相同的 32 位表示形式,但对其进行符号扩展;最后,扩展符号位被设置在n
上。Bit Twiddling Hacks 建议了另一种可能更通用的解决方案。
n ^ 0x80000000
翻转32位符号位;那么- 0x80000000
将符号扩展相反的位。另一种思考方式是,最初,负数位于正数之上(用0x80000000
分隔);^
交换位置;然后-
将负数移至 0 以下。Essentially, the problem is to sign extend from 32 bits to... an infinite number of bits, because Python has arbitrarily large integers. Normally, sign extension is done automatically by CPU instructions when casting, so it's interesting that this is harder in Python than it would be in, say, C.
By playing around, I found something similar to BreizhGatch's function, but that doesn't require a conditional statement.
n & 0x80000000
extracts the 32-bit sign bit; then, the-
keeps the same 32-bit representation but sign-extends it; finally, the extended sign bits are set onn
.Bit Twiddling Hacks suggests another solution that perhaps works more generally.
n ^ 0x80000000
flips the 32-bit sign bit; then- 0x80000000
will sign-extend the opposite bit. Another way to think about it is that initially, negative numbers are above positive numbers (separated by0x80000000
); the^
swaps their positions; then the-
shifts negative numbers to below 0.您正在使用高级脚本语言;本质上,您所运行的系统的本机数据类型是不可见的。您无法使用这样的代码将其转换为本机有符号 int。
如果您知道希望将值转换为 32 位有符号整数(无论平台如何),您只需使用简单的数学运算即可进行转换:
You're working in a high-level scripting language; by nature, the native data types of the system you're running on aren't visible. You can't cast to a native signed int with code like this.
If you know that you want the value converted to a 32-bit signed integer--regardless of the platform--you can just do the conversion with the simple math:
我可以建议这个吗:
Can I suggest this:
您可以使用struct库来转换这样的值。它很丑陋,但有效:
You may use struct library to convert values like that. It's ugly, but works:
一个快速但肮脏的解决方案(在我的例子中 x 永远不会大于 32 位)。
A quick and dirty solution (x is never greater than 32-bit in my case).
如果您知道原始值中有多少位,例如来自 I2C 传感器的字节或多字节值,那么您可以执行标准 二进制补码转换:
If you know how many bits are in the original value, e.g. byte or multibyte values from an I2C sensor, then you can do the standard Two's Complement conversion:
如果该数字的十六进制表示为 4 个字节,则可以解决该问题。
In case the hexadecimal representation of the number is of 4 bytes, this would solve the problem.
任意位长度数字的最简单解决方案
为什么有符号整数的语法对人类来说如此难以理解。因为这是机器的想法。 :-)
让我们解释一下。
双向 7 位计数器
如果我们有一个初始状态为000 0000 的
,我们会得到一个用于向后计数输入的脉冲。那么下一个要数的数字将是
111 1111
人们说:
柜台补充道:
人们问,
计数器回答:找一个比读数大一的数,减去它,就得到结果。
我希望我有帮助
Simplest solution with any bit-length of number
Why is the syntax of a signed integer so difficult for the human mind to understand. Because this is the idea of machines. :-)
Let's explain.
If we have a bi-directional 7-bit counter with the initial state
000 0000
and we get a pulse for the back count input. Then the next number to count will be
111 1111
And the people said:
And the counter added:
And people asked,
The counter replied: Find a number one greater than the reading and subtract it and you get the result.
I hope I helped