• <legend id='KBze7'><style id='KBze7'><dir id='KBze7'><q id='KBze7'></q></dir></style></legend>

    1. <i id='KBze7'><tr id='KBze7'><dt id='KBze7'><q id='KBze7'><span id='KBze7'><b id='KBze7'><form id='KBze7'><ins id='KBze7'></ins><ul id='KBze7'></ul><sub id='KBze7'></sub></form><legend id='KBze7'></legend><bdo id='KBze7'><pre id='KBze7'><center id='KBze7'></center></pre></bdo></b><th id='KBze7'></th></span></q></dt></tr></i><div id='KBze7'><tfoot id='KBze7'></tfoot><dl id='KBze7'><fieldset id='KBze7'></fieldset></dl></div>
      <tfoot id='KBze7'></tfoot>

      <small id='KBze7'></small><noframes id='KBze7'>

        <bdo id='KBze7'></bdo><ul id='KBze7'></ul>

      1. 反转 Python 整数的位

        时间:2023-09-11

            <bdo id='aFTHb'></bdo><ul id='aFTHb'></ul>

              <tbody id='aFTHb'></tbody>
            <i id='aFTHb'><tr id='aFTHb'><dt id='aFTHb'><q id='aFTHb'><span id='aFTHb'><b id='aFTHb'><form id='aFTHb'><ins id='aFTHb'></ins><ul id='aFTHb'></ul><sub id='aFTHb'></sub></form><legend id='aFTHb'></legend><bdo id='aFTHb'><pre id='aFTHb'><center id='aFTHb'></center></pre></bdo></b><th id='aFTHb'></th></span></q></dt></tr></i><div id='aFTHb'><tfoot id='aFTHb'></tfoot><dl id='aFTHb'><fieldset id='aFTHb'></fieldset></dl></div>
          • <small id='aFTHb'></small><noframes id='aFTHb'>

                <tfoot id='aFTHb'></tfoot><legend id='aFTHb'><style id='aFTHb'><dir id='aFTHb'><q id='aFTHb'></q></dir></style></legend>

                  本文介绍了反转 Python 整数的位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  给定一个十进制整数(例如 65),如何反转 Python 中的底层位?即以下操作:

                  Given a decimal integer (eg. 65), how does one reverse the underlying bits in Python? i.e.. the following operation:

                  65 → 01000001 → 10000010 → 130
                  

                  看来这个任务可以分解为三个步骤:

                  It seems that this task can be broken down into three steps:

                  1. 将十进制整数转换为二进制表示
                  2. 反转位
                  3. 转换回十进制

                  第 2 步和第 3 步看起来非常简单(请参阅 this 和 this SO 问题与步骤#2) 相关,但我被困在步骤#1.第 1 步的问题是检索完整的十进制表示并填充零(即 65 = 01000001,而不是 1000001).

                  Steps #2 and 3 seem pretty straightforward (see this and this SO question related to step #2), but I'm stuck on step #1. The issue with step #1 is retrieving the full decimal representation with filling zeros (ie. 65 = 01000001, not 1000001).

                  我四处寻找,但似乎找不到任何东西.

                  I've searched around, but I can't seem to find anything.

                  推荐答案

                  int('{:08b}'.format(n)[::-1], 2)
                  

                  您可以指定任何填充长度来代替 8.如果您想要真正花哨,

                  You can specify any filling length in place of the 8. If you want to get really fancy,

                  b = '{:0{width}b}'.format(n, width=width)
                  int(b[::-1], 2)
                  

                  让您以编程方式指定宽度.

                  lets you specify the width programmatically.

                  这篇关于反转 Python 整数的位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:是否有内置或更多 Pythonic 方式来尝试将字符串解 下一篇:Long Int 文字 - 无效的语法?

                  相关文章

                  最新文章

                • <tfoot id='HexAk'></tfoot>

                  <small id='HexAk'></small><noframes id='HexAk'>

                      • <bdo id='HexAk'></bdo><ul id='HexAk'></ul>
                      <i id='HexAk'><tr id='HexAk'><dt id='HexAk'><q id='HexAk'><span id='HexAk'><b id='HexAk'><form id='HexAk'><ins id='HexAk'></ins><ul id='HexAk'></ul><sub id='HexAk'></sub></form><legend id='HexAk'></legend><bdo id='HexAk'><pre id='HexAk'><center id='HexAk'></center></pre></bdo></b><th id='HexAk'></th></span></q></dt></tr></i><div id='HexAk'><tfoot id='HexAk'></tfoot><dl id='HexAk'><fieldset id='HexAk'></fieldset></dl></div>

                    1. <legend id='HexAk'><style id='HexAk'><dir id='HexAk'><q id='HexAk'></q></dir></style></legend>