Python整数的位反转

28 浏览
0 Comments

Python整数的位反转

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

65 → 01000001 → 10000010 → 130

\n这个任务似乎可以分解为三个步骤:\n

    \n

  1. 将十进制整数转换为二进制表示
  2. \n

  3. 反转位
  4. \n

  5. 转换回十进制
  6. \n

\n步骤#2和#3似乎相当简单(参见与步骤#2相关的这个这个 SO问题),但我卡在步骤#1上。步骤#1的问题是如何获取具有填充零的完整十进制表示(即65 = 01000001,而不是1000001)。\n我已经搜索过了,但似乎找不到任何相关的信息。

0
0 Comments

Reversing bits of a Python integer is a common problem in computer programming. The goal is to reverse the binary representation of an integer. This problem often arises when working with low-level operations or when dealing with bitwise operations.

One of the best ways to reverse the bits of an integer is to perform bit by bit shifting. This method involves iterating over each bit of the integer and swapping its position with the corresponding bit from the opposite end. Here is an example implementation in Python:

def reverse_Bits(n, no_of_bits):
    result = 0
    for i in range(no_of_bits):
        result <<= 1
        result |= n & 1
        n >>= 1
    return result
# Example usage: reverse the bits of 12, which is represented as 1100 in binary
print(reverse_Bits(12, 4))

In the above code, the `reverse_Bits` function takes two arguments: `n` (the integer to reverse) and `no_of_bits` (the number of bits in the integer). It initializes a variable `result` to store the reversed bits. The function then iterates `no_of_bits` times, performing the following operations:

1. Left shift the `result` variable by 1 to make room for the next bit.

2. Use the bitwise OR operator (`|`) to set the least significant bit of `result` to the value of the least significant bit of `n` (obtained using the bitwise AND operator (`&`) with 1).

3. Right shift `n` by 1 to move on to the next bit.

After the loop finishes, the `result` variable will contain the reversed bits of the input integer.

In the provided example, the `reverse_Bits` function is used to reverse the bits of the integer 12, which is represented as 1100 in binary. Since 12 is a 4-bit number, the `no_of_bits` argument is set to 4. The result of the function call is 3, which is the binary representation of 0011 - the reversed bits of 1100.

In conclusion, the problem of reversing bits of a Python integer can be solved efficiently by performing bit by bit shifting. This technique allows for the reverse operation to be achieved in a concise and straightforward manner. By understanding and implementing this approach, programmers can manipulate and transform the binary representation of integers effectively.

0
0 Comments

颠倒Python整数位的问题是因为需要将一个整数的二进制位颠倒顺序以实现某种特定的需求。下面是解决这个问题的方法:

在leetcode的一个链接中提到了一种方法,可以通过使用位运算来实现颠倒整数位的操作。具体的代码如下:

def reverse_mask(x):
    x = ((x & 0x55555555) << 1) | ((x & 0xAAAAAAAA) >> 1)
    x = ((x & 0x33333333) << 2) | ((x & 0xCCCCCCCC) >> 2)
    x = ((x & 0x0F0F0F0F) << 4) | ((x & 0xF0F0F0F0) >> 4)
    x = ((x & 0x00FF00FF) << 8) | ((x & 0xFF00FF00) >> 8)
    x = ((x & 0x0000FFFF) << 16) | ((x & 0xFFFF0000) >> 16)
    return x

然而,原始的链接已经失效,因此无法获取详细的信息。但是,可以在stackoverflow上找到类似的解决方法:stackoverflow.com/a/30002555/9439097

这个问题的解决方法是通过使用位运算来颠倒整数的二进制位的顺序。具体的步骤是将整数按照一定规则进行分组,并使用位运算进行交换。这种方法可以适用于普通的整数,但对于大数类型的数字是否适用尚不清楚。

在stackoverflow上的链接提供了更多关于这个问题的信息,可以在那里找到详细的解释和示例代码。

0
0 Comments

反转Python整数的位的问题是因为需要将整数的二进制表示反转过来。解决方法可以使用字符串格式化和切片操作来实现。

代码中的int('{:08b}'.format(n)[::-1], 2)可以将整数n转换为8位二进制表示的字符串,然后使用切片操作[::-1]将字符串反转,最后使用int函数将反转后的二进制字符串转换为整数。

如果想要指定填充长度,可以将8替换为其他数字。如果想要动态指定填充长度,可以使用'{:0{width}b}'.format(n, width=width)的格式化字符串,其中width为填充长度的变量。

这种方法简洁而优雅。需要注意的是,为了按照指定的规范工作,需要将格式化字符串改为'{:08b}'

某些情况下,如果使用int('{:b}'.format(65)[::-1], 2),只能得到65作为输出。而使用{:08b}代替{:b}可以得到正确的结果,因此对于这个优雅的解决方法给予+1的评价。

可以看出,这里使用的是列表切片操作[::-1],这会导致列表按照反向顺序迭代。

0