Integer equivalence of binary number that is represented as an integer in Python

Question:

I have an input a=int(1010). I want to find integer equivalence of binary 1010. If I use bin(a) then output will be 1111110010, but I want to get 10.

Asked By: Lee

||

Answers:

You need to tell python, that your integer input is in binary. You can either parse a string with a defined base, or ad a 0b-prefix to your code constants.

a = int("1010", base=2)
a = 0b1010
print(a)      # result: 10
print(bin(a)) # result: 1010
Answered By: Jeanot Zubler
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.