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
latteczy
V2EX  ›  Python

python 中负数的与运算为什么不对?-1&2 = 2

  •  
  •   latteczy · 2015-06-03 18:32:03 +08:00 · 3562 次点击
    这是一个创建于 3254 天前的主题,其中的信息可能已经有所发展或是发生改变。
    版本Python 2.7。
    (-1)&2,(-1)&(2)都试过了,结果都是2,不知为何?
    5 条回复    2015-06-11 14:26:08 +08:00
    ayang23
        1
    ayang23  
       2015-06-03 18:38:28 +08:00
    没看出哪不对,-1所有的位都是1,和任何数字与运算结果都是那个数字
    lilydjwg
        2
    lilydjwg  
       2015-06-03 18:40:00 +08:00
    我没看出来哪里不对了……你觉得它的结果是多少才对呢?
    latteczy
        3
    latteczy  
    OP
       2015-06-03 18:44:44 +08:00
    @ayang23
    @lilydjwg
    不好意思各位,脑子进水了。负数的二进制需要取反再加1。
    艾玛,丢人丢大了。
    weyou
        4
    weyou  
       2015-06-03 18:48:37 +08:00
    因为结果本来就是2
    mhohai
        5
    mhohai  
       2015-06-11 14:26:08 +08:00
    很2很2
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2249 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 02:33 · PVG 10:33 · LAX 19:33 · JFK 22:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.