# Python – Convert Binary tuple to Integer

• Last Updated : 19 Sep, 2022

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```

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`

My Personal Notes arrow_drop_up
Related Articles