Python – Convert Binary tuple to Integer
Last Updated :
03 Mar, 2023
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
test_tup = ( 1 , 1 , 0 , 1 , 0 , 0 , 1 )
print ( "The original tuple is : " + str (test_tup))
res = int ("".join( str (ele) for ele in test_tup), 2 )
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
test_tup = ( 1 , 1 , 0 , 1 , 0 , 0 , 1 )
print ( "The original tuple is : " + str (test_tup))
res = 0
for ele in test_tup:
res = (res << 1 ) | ele
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
test_tup = ( 1 , 1 , 0 , 1 , 0 , 0 , 1 )
print ( "The original tuple is : " + str (test_tup))
x = list ( map ( str , test_tup))
x = "".join(x)
res = int (x, 2 )
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
test_tup = ( 1 , 1 , 0 , 1 , 0 , 0 , 1 )
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
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:
- Initialize a variable ‘res’ to 0 to store the decimal number representation of the binary tuple.
- 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.
- The result stored in ‘res’ is the decimal number representation of the binary tuple.
Python3
test_tup = ( 1 , 1 , 0 , 1 , 0 , 0 , 1 )
print ( "The original tuple is : " + str (test_tup))
res = 0
for bit in test_tup:
res = (res << 1 ) | bit
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:
- Check if the length of the input binary tuple is zero. If it is, return 0.
- 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
- function with the rest of the tuple (excluding the first element).
- 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 :])
test_tup = ( 1 , 1 , 0 , 1 , 0 , 0 , 1 )
print ( "The original tuple is : " + str (test_tup))
res = binary_tuple_to_int(test_tup)
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.
Please Login to comment...