按位xor 0xFFFFFFFF? [英] Bitwise xor 0xFFFFFFFF?

查看:83
本文介绍了按位xor 0xFFFFFFFF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决这个问题:

def expr(a):
    return ~(a ^ 0xFFFFFFFF), a ^ 0xFFFFFFFF, ~a, a

print(expr(0xFFFFFFFF))
print(expr(1))
print(expr(0))  
print(expr(-1))

我了解〜a 表示 a 的二进制补码,但是 a ^ 0xFFFFFFFF 也会翻转所有位,但是python会将其解释为大量.我知道Python3使用的是未绑定整数大小,这如何工作?有人可以ELI5(像我五岁一样解释)吗?

I understand ~a means two's complement of a, but a ^ 0xFFFFFFFF also flips all the bits, but python will interpret it as a large number. I know Python3 is using unbound integer size, how does that work? Can someone ELI5 (Explain Like I'm Five)?

结果:

(         -1,           0, -4294967296, 4294967295)
(-4294967295,  4294967294,          -2,          1)
(-4294967296,  4294967295,          -1,          0)
( 4294967295, -4294967296,           0,         -1)

更新:我想我的问题可以简化为:在C中​​,111 ... 1可以表示-1,我明白了,因为它是32位.在Python中,整数大小是无限的,您如何用二进制表示-1?111 ... 1是一个大的正整数,对吗?

UPDATE: I guess my question can be reduced to this: in C, 111...1 can represent -1, I got this, because it's 32 bits. In Python, the integer size is unlimited, how do you represent -1 in binary? 111...1 is a large positive integer, no?

推荐答案

在Python中,整数大小是无限的,您如何用二进制表示-1?111 ... 1是一个大的正整数,对吗?

In Python, the integer size is unlimited, how do you represent -1 in binary? 111...1 is a large positive integer, no?

正数具有无限的前导零序列.也就是说,如果我们需要表示数字100101(等于37),则等于... 000100101,前面有任意多个零.没有计算机系统会尝试存储所有这些前导零,因为存在无限多个,但是可能会存储一些前导零,以便将数字填充到合理"的大小(例如32或64位).

Positive numbers have an infinite sequence of leading zeros. That is, if we need to represent the number 100101 (equal to 37), that's equal to ...000100101, with as many zeros as you like in front. No computer system tries to store all of those leading zeros, because there are infinitely many, but some leading zeros might get stored in order to pad the number out to a "reasonable" size, such as 32 or 64 bits.

Python通过将负数表示为无穷大的前导 ,将其扩展为负数.因此,如果您需要代表-37,则应为... 111011011,前面您需要的数目就可以.同样,-1只是... 1111.因此,如果将XOR -1与另一个Python整数进行XOR运算,它将翻转该数字的所有 all 位,包括其前导零或一个零(就像您使用了tilde运算符一样).不幸的是,Python没有方便的二进制表示法来表示无限前导",因此您不能(例如)将 0b ... 111 编写为整数文字.您必须改用 -1 或将其反转并写入〜0b0 .

Python extends this concept to negative numbers by saying that they have an infinite number of leading ones. So if you need to represent -37, that would be ...111011011, with as many ones as you need in front. Similarly, -1 is just ...1111. So if you XOR -1 with another Python integer, it will flip all of that number's bits, including its leading zeros or ones (as if you had used the tilde operator). Unfortunately, Python does not have a convenient binary notation for "infinite leading ones," so you cannot write (for instance) 0b...111 as an integer literal; you must use -1 instead, or invert it and write ~0b0.

虽然这听起来很荒谬,但实际上在数学上还是很合理的.在 2-adic数字下,这些带有无限前导序列的序列在形式上等效于相应的负整数.如果您更喜欢基于计算机工程的方法,您可能会想到Python整数会自动符号扩展所需的宽度(或多或少是实际的实现方式).

While this may sound ridiculous, it is actually quite mathematically sound. Under the 2-adic numbers, these sequences with infinite leading ones are formally equivalent to the corresponding negative integers. If you prefer an approach more grounded in computer engineering, you might imagine that the Python integers automatically sign-extend to however wide they need to be (which is more or less the actual implementation).

这篇关于按位xor 0xFFFFFFFF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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