What happens when I use [::-1] with a variable?

Question:

So I have this code:

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

I would like to know exactly what’s going on in this line:

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

What does [::-1] means?

Asked By: jafemm

||

Answers:

It takes the reverse of binary value of n excluding "0b" values in the beginning. For example if you enter 6 for value of n. The binary value would be 0b110 and reverse value excluding 0b would be 011.

Answered By: Nagmat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.