Open In App
Related Articles

Python – Convert Binary tuple to Integer

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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. 



Last Updated : 03 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads