
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Total Bits in a Number Using Python
Python Program to Count Total Bits in a Number
To calculate the total bits in a number, convert it to binary using the bin() function. A bit represents data as 0 or 1, and 8 bits make a Byte. Bits are essential for data transmission, storage, and arithmetic operations, and bitwise operations are also possible.
The output of bin() starts with 0b, followed by the binary digits. To find the total bit count, remove the leading 0b after conversion.
Syntax
Following is the Syntax for the bin() function. Where, number is the number to be converted into binary format
variable_name = bin(number)[2:]
Using the 'bin()' Function
In this example, we converted the given number into binary format using the bin() function, and applied slicing to get the binary bits, then we will find the length of the sliced output.
a = int(input("Enter the number:")) binary = bin(a)[2:] print("The binary format of",a,":",binary) print("The number of bits of",binary,":",len(binary))
Following is the output for the above code.
Output
Enter the number:4 The binary format of 4 : 100 The number of bits of 100 : 3
Converting Decimal to Binary
Let's see one more example to get the count of total bits of the given number. The following is the code.
a = 2201 binary = bin(a)[2:] print("The binary format of",a,":",binary) print("The number of bits of",binary,":",len(binary))
Output
The binary format of 2201 : 100010011001 The number of bits of 100010011001 : 12
Counting Bits Using 'bin()' Function
Let's see another example to get the number of bits from the number.
a = int(input("Enter the number:")) binary = bin(a)[2:] print("The binary format of",a,":",binary) print("The number of bits of",binary,":",len(binary))
Output
The following is the output of the number of bits of the number. In the output we can observe the length of the bits of the number.
Enter the number:1407 The binary format of 1407 : 10101111111 The number of bits of 10101111111 : 11
Counting Bits For Float Values
When we want to get the count of a given float number, then the error will be raised as the float numbers cannot be converted into binary numbers.
a = 23.5 binary = bin(a)[2:] print("The binary format of",a,":",binary) print("The number of bits of",binary,":",len(binary))
Output
The following is the output of the number of bits of the float number.
TypeError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_3148\1762579850.py in <module> 1 a = 23.5 ----> 2 binary = bin(a)[2:] 3 print("The binary format of",a,":",binary) 4 print("The number of bits of",binary,":",len(binary)) 5 TypeError: 'float' object cannot be interpreted as an integer
Note ? We are slicing the first 2 values of the converted number because after conversion the representation of binary will be included in the binary number as 0b. So, to get the binary number we have to slice the first 2 values. Only integer data type numbers can be converted into binary numbers.