📜  Python中正整数的反转位

📅  最后修改于: 2022-05-13 01:55:39.222000             🧑  作者: Mango

Python中正整数的反转位

给定一个正整数和位大小,反转它的所有位并返回具有反转位的数字。示例:

Input : n = 1, bitSize=32
Output : 2147483648  
On a machine with size of 
bit as 32. Reverse of 0....001 is
100....0.

Input : n = 2147483648, bitSize=32
Output : 1  

我们可以在Python中快速解决这个问题。方法很简单,

  1. 使用 bin(num)函数将整数转换为二进制表示。
  2. bin()函数在数字的二进制表示中附加0b作为前缀,跳过二进制表示的前两个字符并反转字符串的剩余部分。
  3. 正如我们在内存中所知,数字的任何二进制表示在左起最后一个设置位之后都用前导零填充,这意味着我们需要在反转剩余字符串后附加bitSize – len(reversedBits)零个数。
  4. 现在使用int(字符串,base)方法将二进制表示转换为整数。

int() 方法如何工作?

int(字符串,base)方法采用字符串和基数来识别该字符串是指什么数字系统(二进制=2、十六进制=16、八进制=8 等),并相应地将字符串转换为十进制数字系统。例如 ; int('1010',2) = 10。

Python3
# Function to reverse bits of positive 
# integer number
 
def reverseBits(num,bitSize):
    # Convert number into binary representation
    # output will be like bin(10) = '0b10101'
    binary = bin(num)
     
    # Skip first two characters of binary
    # representation string and reverse
    # remaining string and then append zeros
    # after it. binary[-1:1:-1]  --> start
    # from last character and reverse it until
    # second last character from left
    reverse = binary[-1:1:-1]
    reverse = reverse + (bitSize - len(reverse))*'0'
     
    # converts reversed binary string into integer
    print (int(reverse,2))  
     
# Driver program
if __name__ == '__main__':
    num = 1
    bitSize = 32
    reverseBits(num, bitSize)


Python3
# Python code to implement the approach
 
# Function to find the reverse of the number
def reverse_bits(number, bit_size):   
    # for example, if bitSize is 32   
    # then after 1 << bitSize we will get
    # a 1 in 33-th bit position   
    # bin(1 << bitSize) looks like
    # '0b100000000000000000000000000000000'   
    # so to get all 1 in each 32 bit positions,
    # we need to subtract 1 from (1 << bitSize)
    max_value = (1 << bit_size) - 1
     
    # it is the maximum value for unsigned int32
    # then just subtract your number from the maximum
    return max_value - number
 
if __name__ == "__main__":
    # for example we can get the number 56
    num = 156
     
    # chose a binary size which we want to reverse
    size = 32
    print(reverse_bits(num, size))


输出
2147483648

另一种在不转换为字符串的情况下还原位的方法:

按照以下步骤来实现这个想法:

  • 找到可以由给定数字组成的最大数字。
  • 从中减去给定的数字。
  • 返回数字。

下面是给定方法的实现。

Python3

# Python code to implement the approach
 
# Function to find the reverse of the number
def reverse_bits(number, bit_size):   
    # for example, if bitSize is 32   
    # then after 1 << bitSize we will get
    # a 1 in 33-th bit position   
    # bin(1 << bitSize) looks like
    # '0b100000000000000000000000000000000'   
    # so to get all 1 in each 32 bit positions,
    # we need to subtract 1 from (1 << bitSize)
    max_value = (1 << bit_size) - 1
     
    # it is the maximum value for unsigned int32
    # then just subtract your number from the maximum
    return max_value - number
 
if __name__ == "__main__":
    # for example we can get the number 56
    num = 156
     
    # chose a binary size which we want to reverse
    size = 32
    print(reverse_bits(num, size))
输出
4294967139