Convert Binary tuple to Integer in Python

Consider a tuple consisting of binary elements such as (1,1,0,1,0). A binary number consists of 1s and 0s to represent an integer. A tuple in Python stores multiple elements inside a single variable. The binary numbers are used in various domains such as to represent the sale of an item on an e-commerce website. The value 1 can represent that the item was sold and 0 would indicate that the item is not sold. It can also be used to represent a network of switches with on and off states. Converting the binary format into an integer can be useful for encoding/decoding or simplifying the design of the networks. Thus in this tutorial, we will learn how to convert binary tuples to integers.

Convert binary tuple to Integer – Method 1

For the first method for converting binary tuple to Integer, we initially convert the Tuple to a String using a user-defined convert() function. This function will iterate over each element of the tuple and convert the binary value into a string using the str() function. Then we keep on appending each element to an empty string. Once we return the final string we make use of the int() function. This function will convert the specified value into its corresponding integer format. The int() function takes two arguments. That is the value that we will convert to an integer and the base represents the number format. Note the base value for decimal or integer is 2.

def convert(tuple):
       
    string = ''
    for element in tuple:
        string = string + str(element)
    return string
 

tuple = (1,1,0,1,0,1)
string= convert(tuple)
print(f"Converting tuple to string: {string}")
integer= int(string,2)
print(f"Converted result of binary tuple to integer: {integer}")

Output:

Converting tuple to string: 110101
Converted result of binary tuple to integer: 53

Convert binary tuple to Integer – Method 2

In the second method, we will learn how to convert the binary tuple to an integer without using any pre-defined function. The actual working of the code is explained through the below-mentioned example:

Suppose the binary number is: 1001111
We mutiply each bit starting from right to left with increasing powers of 2

Integer= 1 *2^6 + 0 *2^5 + 0 *2^4 + 1 *2^3 + 1 *2^2 + 1 *2^1 + 1 *2^0
       = 64 + 0 + 0 + 8 + 4 + 2 + 1
       = 79

We iterate over each element of the tuple using for loop. Then we use a counter variable i starting from 0 to represent the powers of 2. We multiply each element with its corresponding powers of 2 and add them to get the final computed integer of the binary tuple.

binary_tuple = (1, 0, 0, 1, 1, 1, 1)

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: 79

Convert binary tuple to Integer – Method 3

In the third method, we will make use of the left shift bit operator and the OR operator to convert a binary tuple to an Integer. Initially, we iterate over each element of the tuple. Then we apply the bitwise left shift operator over the result. The working of the bitwise left-shift operator is as follows:

Suppose the number =14
The binary form of 14 is: 00001110
We shift each bit to its left and fill the right with zeroes to fill up the void

The binary number after the left shift: 00011100
The integer equivalent of the binary form: 28 (2*14)

Thus we can observe that shifting the bits to left is similar to multiplying the number with powers of 2.

After applying the bitwise operator, we apply the OR operator to simply perform binary addition of the number and get the computed result. The working of the OR operator is as follows:

1 | 1 is equal to 1
1 | 0 is equal to 1
0 | 1 is equal to 1 
0 | 0 is equal to 0

Thus we finally get the resultant integer form of the binary tuple.

binary_tuple = (1, 0, 0, 1, 1, 1, 1)
result= 0
for element in binary_tuple:

  result = (result << 1) | element

print("Final integer result is: " + str(result))

Output:

Final integer result is: 79

Thus we have reached the end of this tutorial. To read more about binary operations in Python refer to the following mentioned link: Python | Binary Operations in NumPy

Leave a Reply

Your email address will not be published.