# Python program to find the number of required bits to represent a number in O(1) complexity

Here, we will learn how to find the number of required bits to represent anumber in O(1) complexity using python?
Submitted by Ankit Rai, on June 15, 2019

Problem statement

Find total Number of bits required to represent a number in binary

Example 1:

```    input : 10
output: 4
```

Example 2:

```    input  : 32
output : 6
```

Formula used:

```    Bits_required = floor(log2(number) + 1)
```

Code:

```# From math module import log2 and floor function
from math import log2,floor

# Define a function for finding number of bits
# required to represent any number
def countBits(Num) :

bits = floor(log2(Num) + 1)

return bits

if __name__ == "__main__" :

# assign number
Num = 10

# function call
print(countBits(Num))

Num = 32

print(countBits(Num))
```

Output

```4
6
```

TOP Interview Coding Problems/Challenges

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates