Python如何推断出在二进制中使用的位数量?
我将其从C转换为Python:
int_fast16_t x;
int_fast16_t y = compute_value();
x = ~y;
y = x+1;
我认为不会
y = compute_value()
y = (~y) + 1
有效:在不应该完成二进制的几位上,它怎么知道? (8?16?32?)。
对于C,它可以使用类型 int_fast16_t
进行明确说明,但是使用Python,我们不知道有很多位。
您如何在Python中这样做?
我读过 python?但是这里更具体: python如何推断出在二进制中使用的位数?
示例:
python如何知道二进制于 3 <
3 << /code>(
0B11
)应在4位上完成: 0b00
或8位: 0B11111100
或16位: 0B1111111111111100
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Python
int
具有无限数量的位。将它们倒置的唯一方法是消除数字的迹象。因此,例如〜1
是-2
。要在结果中获取特定数量的位,您必须掩盖这些位。(〜1)&amp; 0xffff
是0xfffe
,这是一点点,我假设由于两个人的补充。Python
int
has an infinite number of bits. The only way to invert them all is to negate the sign of the number. So for example~1
is-2
. To get a specific number of bits in the result, you have to mask off those bits.(~1)&0xffff
is0xfffe
which is one bit off, I assume due to two's complement.它在其对操作数具有的二进制表示中使用所有位。 python整数由一个或多个
long
s组成(请参阅)。在没有情况下只有4或8位。它是在上在整数中拥有的64位中的所有在(如果原始值都需要更长的单词)中完成的。
It uses all bits in the binary representation it has for the operand. Python integers consist of one or more
long
s (see Python integer objects).There is no case where it would be just 4 or 8 bits. It is done on all bits in the 64 bits it has for the integer (or a multiple of that, if the original value needed more long words).