V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2018-09-21 14:57:21 +08:00 · 1799 次点击
    这是一个创建于 2036 天前的主题,其中的信息可能已经有所发展或是发生改变。
    simonliu2018
        1
    simonliu2018  
       2018-09-21 16:42:38 +08:00
    首先 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
       2018-09-21 17:19:58 +08:00
    我指的是二进制的位数,如一下两个 64 位二进制
    hash1 = '0b0010000110100001101101010101111110001001100001101011111110101000'
    hash2 = '0b0010000110101001101101010101111110101001100001101011101110101000'

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

    异或之后的位数不是 64 位了,不清楚中间的换算逻辑是怎样的
    trueGate
        3
    trueGate  
       2018-09-21 18:34:43 +08:00
    @jiangjunhui4000 Python 没有位数的概念,只是省去了前面的 0 位,如果强制 64 位,可以用:`(bin(int(hash1,2)^int(hash2,2))[2:]).zfill(64)`
    ysc3839
        4
    ysc3839  
       2018-09-21 18:36:44 +08:00 via Android
    @jiangjunhui4000 因为转换为文本输出的时候会把开头的 0 省略掉。你自己补回去就好了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4150 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 01:01 · PVG 09:01 · LAX 18:01 · JFK 21:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.