Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Convert Floating to Binary - Python
Next article icon

Python – Convert Binary tuple to Integer

Last Updated : 03 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
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. 



Next Article
Convert Floating to Binary - Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python tuple-programs
Practice Tags :
  • python

Similar Reads

  • 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 tas
    5 min read
  • Convert Tuple to Json Array in Python
    Python's versatility as a programming language extends to its rich data structures, including tuples and JSON. JSON, abbreviation for JavaScript Object Notation, is a lightweight data format used for representing structured data. Moreover, it is a syntax for storing and exchanging data. In this arti
    3 min read
  • Convert Floating to Binary - Python
    The task of converting a floating-point number to its binary representation in Python involves representing the number in the IEEE 754 format, which consists of a sign bit, an exponent and a mantissa. For example, given the floating-point number 10.75, its IEEE 754 32-bit binary representation is "0
    3 min read
  • Convert List to Tuple in Python
    The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
    2 min read
  • 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
    7 min read
  • Python - Ways to convert hex into binary
    We are given a hexadecimal number we need to convert it to binary. For example a = "1A3" we need to convert it to binary so that resultant output should be 110100011. Using bin() and int()We can convert hexadecimal to binary by first converting it to an integer using int() and then using the bin() f
    1 min read
  • Integer to Binary String in Python
    We have an Integer and we need to convert the integer to binary string and print as a result. In this article, we will see how we can convert the integer into binary string using some generally used methods. Example: Input : 77Output : 0b1001101Explanation: Here, we have integer 77 which we converte
    4 min read
  • Python | Convert Integral list to tuple list
    Sometimes, while working with data, we can have a problem in which we need to perform type of interconversions of data. There can be a problem in which we may need to convert integral list elements to single element tuples. Let's discuss certain ways in which this task can be performed. Method #1 :
    3 min read
  • Python | Ways to convert Boolean values to integer
    Given a boolean value(s), write a Python program to convert them into an integer value or list respectively. Given below are a few methods to solve the above task. Convert Boolean values to integers using int() Converting bool to an integer using Python typecasting. C/C++ Code # Initialising Values
    3 min read
  • Python - Interconvert Tuple to Byte Integer
    Sometimes, while working with Python data, we can have a problem in which we need to perform conversion of tuple values, into combined byte and then to integer and vice-versa. This kind of problem can have application in data domains. Let's discuss certain ways in which this task can be performed. I
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences