Open In App

Python – Convert Binary tuple to Integer

Last Updated : 03 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given Binary Tuple representing binary representation of a number, convert to integer.

Input : test_tup = (1, 1, 0) 
Output : 6 
Explanation : 4 + 2 = 6. 
Input : test_tup = (1, 1, 1) 
Output : 7 
Explanation : 4 + 2 + 1 = 7.

Method #1 : Using join() + list comprehension + int()

In this, we concatenate the binary tuples in string format using join() and str(), then convert to integer by mentioning base as 2.

Python3




# Python3 code to demonstrate working of
# Convert Binary tuple to Integer
# Using join() + list comprehension + int()
 
# initializing tuple
test_tup = (1, 1, 0, 1, 0, 0, 1)
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# using int() with base to get actual number
res = int("".join(str(ele) for ele in test_tup), 2)
 
# printing result
print("Decimal number is : " + str(res))


Output

The original tuple is : (1, 1, 0, 1, 0, 0, 1)
Decimal number is : 105

Method #2: Using bit shift and | operator

In this we perform left bit shift and use or operator to get binary addition and hence compute the result.

Python3




# Python3 code to demonstrate working of
# Convert Binary tuple to Integer
# Using bit shift and | operator
 
# initializing tuple
test_tup = (1, 1, 0, 1, 0, 0, 1)
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
 
res = 0
for ele in test_tup:
 
    # left bit shift and or operator
    # for intermediate addition
    res = (res << 1) | ele
 
# printing result
print("Decimal number is : " + str(res))


Output

The original tuple is : (1, 1, 0, 1, 0, 0, 1)
Decimal number is : 105

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3 : Using list(),map(),join(),int() methods

Python3




# Python3 code to demonstrate working of
# Convert Binary tuple to Integer
 
# initializing tuple
test_tup = (1, 1, 0, 1, 0, 0, 1)
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# using int() with base to get actual number
x = list(map(str, test_tup))
x = "".join(x)
res = int(x, 2)
 
# printing result
print("Decimal number is : " + str(res))


Output

The original tuple is : (1, 1, 0, 1, 0, 0, 1)
Decimal number is : 105

Method #4: Using for loop

Python3




# Python3 code to demonstrate working of
# Convert Binary tuple to Integer
 
# initializing tuple
test_tup = (1, 1, 0, 1, 0, 0, 1)
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
res = 0
j = 0
 
for i in range(len(test_tup), 0, -1):
    x = 2**j
    res += x*test_tup[i-1]
    if(j > len(test_tup)):
 
        break
 
    j += 1
 
# printing result
print("Decimal number is : " + str(res))


Output

The original tuple is : (1, 1, 0, 1, 0, 0, 1)
Decimal number is : 105

Method: Using pow() function

Python3




binary_tuple = (1, 1, 0)
result = 0
length = len(binary_tuple)
for i in range(length):
    element = binary_tuple[length - i - 1]
    result = result + element*pow(2, i)
print("The output integer is:", result)


Output

The output integer is: 6

Method#5: Using bit shifting and bitwise operations

Approach:

  1. Initialize a variable ‘res’ to 0 to store the decimal number representation of the binary tuple.
  2. Traverse the binary tuple and do the following:
    a. Shift ‘res’ one bit to the left.
    b. OR ‘res’ with the current bit of the binary tuple.
  3. The result stored in ‘res’ is the decimal number representation of the binary tuple.

Python3




# initializing tuple
test_tup = (1, 1, 0, 1, 0, 0, 1)
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# using bit shifting and bitwise operations to get actual number
res = 0
for bit in test_tup:
    res = (res << 1) | bit
 
# printing result
print("Decimal number is : " + str(res))


Output

The original tuple is : (1, 1, 0, 1, 0, 0, 1)
Decimal number is : 105

Time Complexity:
The time complexity of this approach is O(n), where n is the length of the binary tuple. This is because the binary tuple is traversed only once.

Auxiliary Space:
The space complexity of this approach is O(1), as we are not using any additional data structures to store the intermediate results. We are using a single variable ‘res’ to store the decimal number representation of the binary tuple.

Method#6: Using Recursive method.

Algorithm:

  1. Check if the length of the input binary tuple is zero. If it is, return 0.
  2. If the length of the tuple is not zero, calculate the decimal value of the first element of the tuple by multiplying it with 2 raised to the power of the length of the tuple minus one, and add this value to the result of a recursive call to the same
  3. function with the rest of the tuple (excluding the first element).
  4. Return the final result.

Python3




def binary_tuple_to_int(binary_tup):
    if len(binary_tup) == 0:
        return 0
    else:
        return binary_tup[0] * 2**(len(binary_tup)-1) + binary_tuple_to_int(binary_tup[1:])
# initializing tuple
test_tup = (1, 1, 0, 1, 0, 0, 1)
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# calling recursive method
res = binary_tuple_to_int(test_tup)
 
# printing result
print("Decimal number is : " + str(res))


Output

The original tuple is : (1, 1, 0, 1, 0, 0, 1)
Decimal number is : 105

The time complexity of this algorithm is O(n), where n is the length of the binary tuple, since we are making a recursive call for each element in the tuple. 

The auxiliary space is also O(n), since the function call stack will contain n recursive calls at its maximum depth. 



Similar Reads

Python - Convert Tuple String to Integer Tuple
Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + int() + replace() + split() The combination of above methods can be used to perform this task. In this, we perform th
7 min read
Python - Convert Tuple to Tuple Pair
Sometimes, while working with Python Tuple records, we can have a problem in which we need to convert Single tuple with 3 elements to pair of dual tuple. This is quite a peculiar problem but can have problems in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ('A'
10 min read
Python Program to Convert Tuple Matrix to Tuple List
Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]] Output : [(4, 10), (5, 13)] Explanation : All co
8 min read
Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set method is to convert other type values to set this meth
7 min read
Python | Convert Tuple to integer
Sometimes, while working with records, we can have a problem in which we need to convert the data records to integer by joining them. Let's discuss certain ways in which this task can be performed. Method #1 : Using reduce() + lambda The combination of above functions can be used to perform this task. In this, we use lambda function to perform logi
5 min read
Python | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Python - Raise elements of tuple as power to another tuple
Sometimes, while working with records, we can have a problem in which we may need to perform exponentiation, i.e power of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1: Using zip() + generator expression The combination of above functions can be used to perform this
8 min read
Python - Flatten tuple of List to tuple
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform the flattening of tuples, which have listed as their constituent elements. This kind of problem is common in data domains such as Machine Learning. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ([5], [6], [3], [8]) O
7 min read
Python Program to find tuple indices from other tuple list
Given Tuples list and search list consisting of tuples to search, our task is to write a Python Program to extract indices of matching tuples. Input : test_list = [(4, 5), (7, 6), (1, 0), (3, 4)], search_tup = [(3, 4), (8, 9), (7, 6), (1, 2)]Output : [3, 1]Explanation : (3, 4) from search list is found on 3rd index on test_list, hence included in r
8 min read
Practice Tags :