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
.
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
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
.
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