当我将[::-1]与变量一起使用时会发生什么?

问题描述

所以我有这个代码

t=int(input())
while t:
 s=int(input())
 n=bin(s)
 n=n[2:][::-1]
 if n.count('1')==1:
  pos=n.find('1')+1
  print(pos)
else:
  print('-1')
t-=1

我想确切知道此行中发生了什么:

 n=n[2:][::-1]

[::-1]是什么意思?

解决方法

以n的二进制值的倒数开头,其中不包括“ 0b”值。例如,如果输入6作为n的值。二进制值为0b110,不包括0b的反向值为011。