Python program to print the number of bits to store an integer and also print number in Binary format

Here, we are executing a python program that will print the quantities of bits to store an integer number and furthermore print its double value.

Given an integer number and we need to print quantities of bits to store the number and its twofold value.

Printing number of bits to store an integer:

To locate the all outnumber of bits to store an integer, we use bit_length() work, it is called with the number (an integer value) and returns the complete number of bits to store the given number.

Printing binary value:

To print binary value of a given integer, we use bin() function it acknowledges the number as contention and returns the parallel value.

Example:

    Input:
    num = 10

    Output:
    Number of bits to store the number: 4
    Binary value: 0b1010

Python code:

# Python program to print number of bits to store an integer 
# and also print number in Binary format

# input a number 
num = int(input("Enter an integer number: "))

# print the input number
print("Entered number is: ", num)

# printing number of bits to store the number
print(num, " needs ", num.bit_length(), " to store the value")

# printing binary value 
print("Binary value of ", num, " is: ", bin(num))

Output:

First run:
Enter an integer number: 120
Entered number is:  120
120  needs  7  to store the value
Binary value of  120  is:  0b1111000

Second run:
Enter an integer number: 10
Entered number is:  10
10  needs  4  to store the value
Binary value of  10  is:  0b1010

Leave a Comment

error: Alert: Content is protected!!