Example:
tuple = ("python", "includehelp", 43, 54.23)
Converting Binary Tuple to Integer
We are given a tuple consisting of only binary values (0 or 1). Using this tuple, we need to convert the tuple into an integer.
Input:
tup = (1, 0, 1, 1, 0)
Output:
22
Method 1:
One method to solve the problem is by using the bitwise shift operator to left shift bits of the number and find the binary addition using the or operator to find the resulting value.
Output:
Method 2:
Another method to solve the problem is by using list comprehension and formatting the binary values of the tuple using join() and str() methods.
After creating the string, we will convert it back to an integer using base 2.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer