Skip to content
Related Articles

Related Articles

Improve Article

Python Bin | Count total bits in a number

  • Difficulty Level : Basic
  • Last Updated : 01 Sep, 2020

Given a positive number n, count total bit in it.


Input : 13
Output : 4
Binary representation of 13 is 1101

Input  : 183
Output : 8

Input  : 4096
Output : 13


We have existing solution for this problem please refer Count total bits in a number link. We can solve this problem quickly in Python using bin() function. Convert number into it’s binary using bin() function and remove starting two characters ‘0b’ of output binary string because bin function appends ‘0b’ as prefix in output string. Now print length of binary string that will be the count of bits in binary representation of input number.


# Function to count total bits in a number
def countTotalBits(num):
     # convert number into it's binary and
     # remove first two characters 0b.
     binary = bin(num)[2:]
# Driver program
if __name__ == "__main__":
    num = 13




 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :