{"id":1284,"date":"2019-10-02T21:25:28","date_gmt":"2019-10-02T13:25:28","guid":{"rendered":"https:\/\/jackyu.cn\/?p=1284"},"modified":"2020-08-31T11:57:20","modified_gmt":"2020-08-31T03:57:20","slug":"128","status":"publish","type":"post","link":"https:\/\/jackyu.cn\/algorithm\/128\/","title":{"rendered":"-128 \u7684\u516b\u4f4d\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801"},"content":{"rendered":"<div class=\"nc-light-gallery\">\n<p>\u8fd8\u662f\u5e2e\u540c\u5b66\u89e3\u9898\u7684\u65f6\u5019\u8e29\u5751\u4e86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>\u539f\u7801<\/td><td>\u65e0<\/td><\/tr><tr><td>\u53cd\u7801<\/td><td>\u65e0<\/td><\/tr><tr><td>\u8865\u7801<\/td><td>10000000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4e00\u5f00\u59cb\u6ca1\u53d1\u73b0\uff0c\u4ee5\u81f3\u4e8e\u6bcf\u6b21\u8f93\u5165-128\u7684\u65f6\u5019\u8f6c\u6362\u51fa\u6765\u7684\u683c\u5f0f\u662f\u4e0d\u6b63\u786e\u7684\u3002\u7136\u540e\u81ea\u5df1\u7b97\u7684\u65f6\u5019\u53d1\u73b0\u95ee\u9898\u4e86\uff0c\u516b\u4f4d\u539f\u7801\u91cc\u9762\u7b2c\u4e00\u4f4d\u662f\u7b26\u53f7\u4f4d\uff0c\u5269\u4e0b7\u4f4d\u662f\u6570\u503c\u4f4d\uff0c2^7=128 \u7b97\u4e0a\u8d1f\u53f7 256 \u4e2a\u6570\u5b57\uff0c+0\/-0\u5404\u5360\u4e00\u4e2a\uff0c\u6240\u4ee5\u516b\u4f4d\u4e8c\u8fdb\u5236\u539f\u7801\u8868\u793a\u5341\u8fdb\u5236\u6570\u7684\u8303\u56f4\u4e3a -127~127\u3002<\/p>\n\n\n\n<p>\u8fd9\u8c01\u9876\u5f97\u4f4f\u554a\uff0c\u539f\u9898\u5c31\u662f\u6c42 -128~127\u3002<\/p>\n\n\n\n<p>\u540e\u6765\u67e5\u4e86\u4e0b\uff0c-128 \u6ca1\u6709\u539f\u7801\u548c\u53cd\u7801\uff0c\uff08\u4eba\u4e3a\u89c4\u5b9a\uff09\u8865\u7801\u4e3a1000000(-0)\u3002<\/p>\n\n\n\n<p>\u9644\u4e0a\u539f\u9898\u7684\u89e3\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># coding=utf-8\n\n# \u6c42 -128 ~ 127 \u76848\u4f4d\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\n# Copyright 2019 0xJacky&lt;jacky-943572677@qq.com>\n\nprint(\"Please input a decimal integer between -128 ~ 127:\")\ni = int(input())\n\n# \u9650\u5b9a\u8f93\u5165\u8303\u56f4\uff0c\u5426\u5219\u65e0\u9650\u5faa\u73af\nwhile i &lt; -128 or i > 127:\n    print(\"Please input a decimal integer between -128 ~ 127:\")\n    i = int(input())\n\ninverse = ''\nif i &lt; 0 or i > -128:\n    # \u5bf9\u4e8e -127 &lt; i &lt; 0 \u7684\u6570\u8fdb\u884c\u5904\u7406\n    binary = format(i, '08b')&#91;1:]  # \u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u53bb\u6389\u8d1f\u53f7\uff0c\u56fa\u5b9a\u957f\u5ea6\n    # \u9010\u9879\u6c42\u53cd\uff0c\u5f97\u5230\u53cd\u7801\n    for b in binary:\n        if b == '1':\n            inverse += '0'\n        else:\n            inverse += '1'\n    \n    # \u4e0b\u9762\u52a0\u7b26\u53f7\u4f4d\uff081\uff09\u8bf4\u660e\u662f\u4e2a\u8d1f\u6570\n    binary = '1' + binary\n    # \u4e8c\u8fdb\u5236\u52a0\u6cd5\uff0c\u6c42\u8865\u7801\uff08\u8d1f\u6570\u7684\u8865\u7801=\u53cd\u7801\u672b\u5c3e+1\uff0c\u4e8c\u8fdb\u5236\uff09\n    # int(\u5b57\u7b26\u4e32, \u8fdb\u5236)\n    complement = int(inverse, 2) + int('1', 2)\n    complement = '1' + format(complement, '07b')\n    inverse = '1' + inverse\n            \nelif i >= 0:\n    # \u6b63\u6570\u7684\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u5747\u76f8\u540c\n    # \u8fd9\u91cc\u5f53\u505a\u5b57\u7b26\u4e32\u5904\u7406\u5e76\u52a0\u4e0a\u7b26\u53f7\u4f4d\uff080\uff09\u8bf4\u660e\u8fd9\u4e2a\u662f\u6b63\u6570\n    binary = format(i, '07b')\n    inverse = '0' + binary\n    complement = '0' + binary\n    binary = '0' + binary\n\nelse:\n    # \u5bf9 -128 \u7279\u6b8a\u5904\u7406\uff0c\u56e0\u4e3a -128 \u7684\u6570\u503c\u4f4d\u5df2\u8fbe\u52308\u4f4d\uff0c\u52a0\u4e0a\u7b26\u53f7\u5417\u540e\u5219\u53d8\u4e3a9\u4f4d\n    # \u4e0d\u7b26\u5408\u5b9a\u4e49\uff0c\u5bf9\u6b64\uff0c\u7ecf\u8fc7\u8be2\u95ee\u524d\u8f88\u4ee5\u53ca\u767e\u5ea6\u540e\u5f97\u77e5\uff0c\u5728 -128 \u7684\u539f\u7801\u548c\u8865\u7801\u4e0d\u5b58\u5728\n    # \u4eba\u4e3a\u89c4\u5b9a\u5176\u8865\u7801\u4e3a 10000000\uff08\u5373\u4e3a-0\u7684\u8865\u7801\uff09\n    # \u8865\u5145\u8bf4\u660e\uff0c\u5728\u300a\u5927\u5b66\u8ba1\u7b97\u673a\u300b\u5185\uff0c\u4e00\u822c\u8ba9\u5b66\u751f\u6c42 -127 ~ 127\n    # \u56e0\u4e3a +0, -0 \u7684\u539f\u7801\u3001\u8865\u7801\u3001\u53cd\u7801\u5e76\u4e0d\u76f8\u540c\uff08\u7b26\u53f7\u4f4d\u7684\u5dee\u5f02\uff09\n    binary = 'undefined'\n    inverse = 'undefined'\n    complement = '10000000'\n\nprint(binary, 'bin')\nprint(inverse, 'inv')\nprint(complement, 'comp')\n\n<\/code><\/pre>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>\u8fd8\u662f\u5e2e\u540c\u5b66\u89e3\u9898\u7684\u65f6\u5019\u8e29\u5751\u4e86\u3002 \u539f\u7801 \u65e0 \u53cd\u7801 \u65e0 \u8865\u7801 10000000 \u4e00\u5f00\u59cb\u6ca1\u53d1\u73b0\uff0c\u4ee5\u81f3\u4e8e\u6bcf\u6b21\u8f93\u5165-128 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1683,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[92],"tags":[],"special":[],"class_list":["post-1284","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-algorithm"],"modified_by":"Jacky","_links":{"self":[{"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/posts\/1284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/comments?post=1284"}],"version-history":[{"count":4,"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/posts\/1284\/revisions"}],"predecessor-version":[{"id":1684,"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/posts\/1284\/revisions\/1684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/media\/1683"}],"wp:attachment":[{"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/media?parent=1284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/categories?post=1284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/tags?post=1284"},{"taxonomy":"special","embeddable":true,"href":"https:\/\/jackyu.cn\/wp-json\/wp\/v2\/special?post=1284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}