How to Count Total Number of Bits in Number?

29-Oct-2022

.

Hi Dev,

This tutorial will give you example of How to Count Total Number of Bits in Number. I’m going to show you about Count total bits in a number. In this article, we will implement a Python in Count set bits in an integer. you can see Counting Bits in Python Example.

python program to count total number of bits in a number; In this tutorial, you will learn how to count total number of bits in the number using Python.

let's see below simple example with output:

Example 1: Simple Python Program to Add Two Numbers

```# write a python program to count total number of bits in a number

num = int(input("Please Enter any Number: "))

# use bin () method to get the binary value of a number
# print binary value output will be 0b111101
print ("binary value of {0} is: {1}".format(num, bin(num)))

# store the length of the binary number
length = len(bin(num))

# to get exact length total number of bits
# subtract 2 from the length
length -=2

# print length
print ("total number of bits: ", length)
```

Output:

```Please Enter any Number:  13
binary value of 13 is: 0b1101
total number of bits:  4
```