Q:

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

belongs to collection: Python miscellaneous programs

0

Example:

    Input:
    num = 10

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python miscellaneous programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to print an array of bytes represen... >>
<< Python program to find number of bits necessary to...