推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
jiangjunhui4000
V2EX  ›  Python

python3 对 64 位的二进制进行异或的运算后,结果变成了 32 位的,这是什么逻辑?

  •  
  •   jiangjunhui4000 · Sep 21, 2018 · 2329 views
    This topic created in 2823 days ago, the information mentioned may be changed or developed.
    simonliu2018
        1
    simonliu2018  
       Sep 21, 2018
    首先 python3 里应该没有 32 位、64 位整数,都叫整数:

    In [169]: type(2 ** 64)
    Out[169]: int

    In [170]: type(2 ** 32)
    Out[170]: int

    另外,这个异或结果是 0,那是多少位的整数呢?

    In [171]: (2 ** 64) ^ (2 ** 64)
    Out[171]: 0
    jiangjunhui4000
        2
    jiangjunhui4000  
    OP
       Sep 21, 2018
    我指的是二进制的位数,如一下两个 64 位二进制
    hash1 = '0b0010000110100001101101010101111110001001100001101011111110101000'
    hash2 = '0b0010000110101001101101010101111110101001100001101011101110101000'

    In [51]: bin(int(hash1,2)^int(hash2,2))
    Out[51]: '0b1000000000000000000000100000000000000000010000000000' ##52 位

    异或之后的位数不是 64 位了,不清楚中间的换算逻辑是怎样的
    trueGate
        3
    trueGate  
       Sep 21, 2018
    @jiangjunhui4000 Python 没有位数的概念,只是省去了前面的 0 位,如果强制 64 位,可以用:`(bin(int(hash1,2)^int(hash2,2))[2:]).zfill(64)`
    ysc3839
        4
    ysc3839  
       Sep 21, 2018 via Android
    @jiangjunhui4000 因为转换为文本输出的时候会把开头的 0 省略掉。你自己补回去就好了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1544 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 16:30 · PVG 00:30 · LAX 09:30 · JFK 12:30
    ♥ Do have faith in what you're doing.