A binary tuple is a tuple that contains only 0s and 1s as its elements. In this article, we will discuss different ways to convert a binary tuple to an integer in python.
How to Convert a Binary Tuple to an Integer in Python
Consider that we are given the following binary tuple.
myTuple= (1,1,0,1,1,0,1)
Now, we have to create an integer from this binary tuple. The integer will contain all the digits of the tuple in its binary representation. So, the number will be (1101101)2
. In decimal representation (1101101)2
evaluates to 109
. So, our program should give the output 109
.
To convert a binary number to a decimal number, we multiply the digits of the number with powers of 2. The rightmost bit is multiplied by 20. The second rightmost digit is multiplied by 21, the third digit from the right is multiplied by 22. Similarly, the Nth digit is multiplied by 2N-1. After that, the value from each digit is added and the decimal representation is obtained.
For example, we can convert (1101101)2
to decimal representation as follows.
(1101101)2 = 1x26 +1x25 +0x24+1x23+1x22+0x21+1x20
=64+32+0+8+4+0+1
=109
To implement the above logic to convert a binary tuple to integer in python, we will first initialize a variable myInt
to 0. We will also calculate the length of the tuple using the len()
function. After that, we will traverse the tuple from right to left and multiply the elements with powers of 0 using a for loop, range()
function, and the tuple length. After multiplying, we will add the values to myInt
. After execution of the for loop, we will get the integer output in the myInt
variable. You can observe this in the following example.
myTuple = (1, 1, 0, 1, 1, 0, 1)
myInt = 0
length = len(myTuple)
for i in range(length):
element = myTuple[length - i - 1]
myInt = myInt + element*pow(2, i)
print("The tuple is:", myTuple)
print("The output integer is:", myInt)
Output:
The tuple is: (1, 1, 0, 1, 1, 0, 1)
The output integer is: 109
Convert a Binary Tuple to an Integer Using Strings
We can also use strings to convert a binary tuple to an integer in python. For this, we will first convert all the elements of the tuple to string using the str()
function and map()
function. After that, we will create a string from the elements of the tuple using the join()
method. The join()
method, when invoked on a string, takes an iterable object as input and returns a string consisting of the elements of the iterable. We will first create an empty string and then use the join()
method to obtain the string from tuple as follows.
myTuple = (1, 1, 0, 1, 1, 0, 1)
newTuple = map(str, myTuple)
myStr = "".join(newTuple)
print("The tuple is:", myTuple)
print("The output string is:", myStr)
Output:
The tuple is: (1, 1, 0, 1, 1, 0, 1)
The output string is: 1101101
After obtaining the string, we can directly convert the string to an integer using the int() function as follows.
myTuple = (1, 1, 0, 1, 1, 0, 1)
newTuple = map(str, myTuple)
myStr = "".join(newTuple)
myInt = int(myStr, 2)
print("The tuple is:", myTuple)
print("The output integer is:", myInt)
Output:
The tuple is: (1, 1, 0, 1, 1, 0, 1)
The output integer is: 109
In the code, we have passed the value 2 to the int()
function as the second input argument to show that the string contains bits of a binary number.
Conclusion
In this article, we have discussed two ways to convert a binary tuple to an integer in python. To know more about strings, you can read this article on string concatenation in python. You might also like this article on list comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.